Ask Your Question
0

How to print a text file containing multiple tuples from a function

asked 2015-05-03 18:42:46 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am trying to use Sagemath Cloud to create a text file with multiple columns of data. Each column is the input or output value of the variables of a system of functions being evaluated with for loops. I am very new to Python, and when I try to do this, I either get an error message or I get a text file with only one line, and that line is constantly overwritten by a new value of the function. On my own laptop with its standalone version of Sage, I can easily generate what I want simply by telling Sage to print the values of the variables of the function for each iteration of the for loops. The standalone version even creates an output text file that I can save. How do I do this with the cloud version?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-05-04 08:21:15 -0500

calc314 gravatar image

Here is one possible way to output the results in a csv file.

import csv

# sample function
f(i,j) = i^2*j

with open('tmp.csv', 'w') as f1:
    writefile = csv.writer(f1)

    for i in range(0,10):
        for j in range(0,10):
            result=[i,j,f(i,j)]
            writefile.writerow(result)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-05-03 18:42:46 -0500

Seen: 191 times

Last updated: May 04 '15