Ask Your Question
1

Evaluate from string an equation that has integer division

asked 2018-11-18 11:21:18 -0600

I have the following Python list that contains strings of equations.

L = ['651/349*t + 5382747/9778631000', 't + 57879/196133000', '1000/349*t + 57879/68450417']

You can see that all of the equations have integer division. Because this is obtained by some program, I can not edit the strings.

When I evaluate for t=1.0 with the following code in SageMath (for example), it does not evaluate as an Euclidean division.

F = function('F')(t)
for k in L:
    F(t) = eval(k)
    Fc = fast_callable(F, vars=[t])
    val2eval = 1.0
    print(Fc(val2eval).n(13))

It gives

1.00000000000000
1.00000000000000
2.00000000000000

And should be give

1.86587997307599
1.00029510077345
2.86617507384944

Of course I can solve this by modifying MANUALLY the strings of the equations by indicating that the denominator is a real number and not an integer (I put a decimal point at the end of the integer in the denominator) as following.

L = ['651/349.*t + 5382747/9778631000.', 't + 57879/196133000.', '1000/349.*t + 57879/68450417.']

But this is not the idea, the strings of the equations are generated automatically and one can not modify by editing manually, because it will be implemented in a process where it should be create at least 2000 equations.

Is there some elegant solution for this? --Many thanks!

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2018-11-18 12:48:18 -0600

slelievre gravatar image

One solution is to use Python3-style division, where the quotient of two integers is a floating-point number.

The simplest way is to import division from __future__:

Use .n(digits=13) to get 13 decimal digits; using .n(13) gives 13 bits of precision, so only 3 decimal digits.

sage: from __future__ import division
sage: L = ['651/349*t + 5382747/9778631000', 't + 57879/196133000', '1000/349*t + 57879/68450417']
sage: t = SR.var('t')
sage: F = function('F')(t)
sage: for k in L:
....:     F(t) = eval(k)
....:     Fc = fast_callable(F, vars=[t])
....:     val2eval = 1.0
....:     print(Fc(val2eval).n(digits=13))
....:
1.865879973076
1.000295100773
2.866175073849
edit flag offensive delete link more

Comments

I put the sentence from __future__ import division at the beginning of my file 'twophasevolumeweightunsat.sage' and I become the following Error Message:

sage: load('twophasevolumeweightunsat.sage')
  File "<string>", line 2
SyntaxError: from __future__ imports must occur at the beginning of the file
loSuarezB gravatar imageloSuarezB ( 2018-11-20 10:45:13 -0600 )edit

I'm using 'SageMath version 7.5.1, Release Date: 2017-01-15' because I could not install the new SageMath 8.X., this is another problem I would afford next.

loSuarezB gravatar imageloSuarezB ( 2018-11-20 10:47:28 -0600 )edit

You could try putting the import statement at the start of the init.sage file in the .sage folder in your home directory. Create the init.sage file if it does not exist yet.

slelievre gravatar imageslelievre ( 2018-11-20 11:22:39 -0600 )edit

Or you could convert your file twophasevolumeweightunsat.sage into twophasevolumeweightunsat.py; this might involve modifying your code to turn it into pure Python syntax, and adding imports for all the Sage functions that are used in this file.

slelievre gravatar imageslelievre ( 2018-11-20 11:24:59 -0600 )edit
2

answered 2018-11-20 23:40:18 -0600

nbruin gravatar image

If you want to parse strings in the way that the sage prompt does it, you could use

eval(preparse(k))

It's also a little wasteful to call

F(t)=eval(preparse(k))

(that's not valid python syntax. The preparser does quite a bit with that as well). In fact, if you know you want to parse the strings into symbolic expressions, you might want to call

F=symbolic_expression(k).function(t)

if you want F to be a function in t rather than just an expression.

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: 2018-11-18 11:21:18 -0600

Seen: 92 times

Last updated: Nov 20 '18