Ask Your Question

wjur's profile - activity

2019-03-07 13:31:40 -0500 received badge  Famous Question (source)
2018-12-06 12:12:13 -0500 received badge  Notable Question (source)
2018-05-23 20:07:52 -0500 received badge  Popular Question (source)
2017-03-30 06:42:53 -0500 commented answer Evaluating symbolic expression, when some variables are finally fixed

BTW, more on substituting multiple values is on
Substituting multiple values question&answer

2017-03-30 06:31:47 -0500 commented answer Evaluating symbolic expression, when some variables are finally fixed

BTW, Solution I found thanks to other sage question&answer
problems with sage_eval and eval

2017-03-24 19:20:04 -0500 received badge  Supporter (source)
2017-03-24 19:09:58 -0500 answered a question Evaluating symbolic expression, when some variables are finally fixed

Thanks to other answers, which concentrated on particular example, I wanted to explain better what I'm after (i.e. the general solution), and in that process I've got the answer:

I = eval(preparse(str(I)))

where

  1. str(I) converts the 'I' expression to string
  2. preparse(...) preparses the code of expression in the sage style ('^' as power, and so on)
  3. eval(...) finally evaluates expression (in particular example: taking into account that R is already set to 3)

The code thus would be:

V = var('V')
R = var('R')
I = V/R
R = 3  # this is one line representation of possible several lines of code
       # to compute value in general case. Let's also note that the value can 
       # even be provided externally (read from file, or so) 
I = eval(preparse(str(I)))
plot(I)

and having in mind even more general solution with evaluate() definition:

def evaluate(expression):
    return eval(preparse(str(expression)))

V = var('V')
R = var('R')
I = V/R
R = 3  # this is one line representation of possible several lines of code
       # to compute value in general case. Let's also note that the value can 
       # even be provided externally (read from file, or so) 
I = evaluate(I)
plot(I)

Few days later:
There is a small improvement possible - by adding simplify(), and so making the whole thing
I = simplify(eval(preparse(str(I))))


Please let know if the code could be more elegant (pythonic or sageonic) and if there are some disadvantages or threads in such solution

2017-03-24 11:58:37 -0500 received badge  Editor (source)
2017-03-24 09:50:15 -0500 received badge  Student (source)
2017-03-24 07:24:23 -0500 asked a question Evaluating symbolic expression, when some variables are finally fixed

Hello

I'm looking for solution when bigger number of variables is in the picture.

Simple example: There is relationship I = V/R then we know that R = 3, with that knowledge let's plot I(V)
in sage it should look something like:

V = var('V')
R = var('R')
I = V/R
R = 3
plot(I)

but above is not working, because in plot(I) I is still V/R not V/3 (knowledge of R=3 is not used), so I'm looking for some operation that turns V/R into V/3, something like

I = evaluate(I)

but such evaluate() apparently does not exist

there are however two different ways
(1) repeat I=V/R after R=3 and in such situation I becomes V/3 as needed
(2) I = I(R=R) and with earlier R=3, I also becomes V/3
both however seems for me be solution(s) for simple expression with just few variables, and not good (not as straightforward as hypothetical I = evaluate(I) for more complex expression with several variables.

Is such evaluate() or so, available already, and I failed to find it?
Is there a (easy) way to implement that proposed evaluate() ?