Ask Your Question
0

How to read a file which has rational functions?

asked 2024-03-24 11:19:42 +0100

lijr07 gravatar image

I have a large file which has rational functions and I would like to read it to Sagemath. The first two lines are

[[[4, -1]], (x7 + x20)/x19]
[[[4, -3], [4, -1]], (x8*x19 + x7*x21 + x20*x21)/(x19*x20)]

When I tried to read it, sagemath said that x_i's are not defined. Thank you very much!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-03-24 11:41:46 +0100

rburing gravatar image

Here is how to read them line-by-line from a string:

funs = """[[[4, -1]], (x7 + x20)/x19]
[[[4, -3], [4, -1]], (x8*x19 + x7*x21 + x20*x21)/(x19*x20)]"""

R = PolynomialRing(QQ, names=[f"x{k}" for k in range(1,22)])

for line in funs.split('\n'):
    value = sage_eval(line, locals=R.gens_dict())
    print(value)

Output:

[[[4, -1]], (x7 + x20)/x19]
[[[4, -3], [4, -1]], (x8*x19 + x7*x21 + x20*x21)/(x19*x20)]

You can adapt it to read lines from a file.

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: 2024-03-24 11:19:42 +0100

Seen: 144 times

Last updated: Mar 24