python sympy不能在使用字母I时替代数值。

huangapple go评论60阅读模式
英文:

python sympy cannot sub value when using letter I

问题

I am using sympy expressions to rearrange and solve equations I have been given.

from sympy import *

V = symbols("V")
I = symbols("I")
R = symbols("R")
expr = "R*I-V"
expr = parse_expr(expr)

expr = expr.subs("I", 20)
expr = expr.subs("R", 10)

answer = solve(expr, V)
print(answer)

This should output 200 but instead outputs I*10.

For some reason, it does not substitute the value for I.

When a different value is used, it works fine:

V = symbols("V")
i = symbols("i")
R = symbols("R")
expr = "R*i-V"
expr = parse_expr(expr)

expr = expr.subs("i", 20)
expr = expr.subs("R", 10)

answer = solve(expr, V)
print(answer)

This outputs the correct value of 200.

Other values work, such as other capital and non-capital letters.

Could someone explain if there is something I have missed or if this behavior is strange?

As I mentioned before, I tried using a different value, and it worked as expected.

Rearranging the entered equation did nothing, so expr = "R*I-V" had the same output as expr = "I*R-V".

英文:

I am using sympy expressions to rearrange and solve equations i have been given.

from sympy import *

V = symbols("V")
I = symbols("I")
R = symbols("R")
expr = "R*I-V"
expr = parse_expr(expr)

expr = expr.subs("I",20)
expr = expr.subs("R",10)

answer = solve(expr,V)
print(answer)

this should output 200 but insted outputs I*10

for some reason it does not substitute the value for I

when a differnt value is used it works fine:

V = symbols("V")
i = symbols("i")
R = symbols("R")
expr = "R*i-V"
expr = parse_expr(expr)

expr = expr.subs("i",20)
expr = expr.subs("R",10)

answer = solve(expr,V)
print(answer)

this outputs the correct value of 200
other values work such as other capitals and non capitals.

Could someone explain if there is something i have missed or if this behaviour is strange

as i mentioned before i tried using a different value and it worked as expected.

rearranging the entered equation did nothing so expr = "R*I-V" had the same output as expr = "I*R-V"

答案1

得分: 2

Sympy使用一些字母来标识特定的对象。具体来说:

  • "O" 是 Order 类的别名。
  • "Q" 是 AssumptionKeys 类的别名。
  • "S" 是 sympify() 函数的别名,或者是单例对象的注册表。
  • "N" 是使用 evalf() 函数进行数值评估的别名。
  • "E" 代表自然对数的底数。
  • "I" 代表虚数单位。

因此,当 parse_expr 遇到字符 "I" 时,它会将其转换为虚数单位!您可以提供 locad_dict 字典,将您的符号关联到字符串中的特定字符。例如:

expr = "R*I-V"
I = symbols("I")
expr = parse_expr(expr, local_dict={"I": I})

expr = expr.subs("I", 20)
expr = expr.subs("R", 10)

answer = solve(expr, V)
print(answer)
# [200]
英文:

Sympy uses some letters to identify specific objects. In particular:

  • "O" is an alias to the Order class.
  • "Q" is an alias for the AssumptionKeys class.
  • "S" is an alias to the sympify() function, or to the registry of singleton objects.
  • "N" is an alias to numerical evaluation with the evalf() function.
  • "E" represents the base of the natural logarithm.
  • "I" represents the imaginary unit.

Hence, when parse_expr sees the "I" character, it converts it to the imaginary unit! You can provide the locad_dict dictionary to associate your symbols to a particular character of the string. For example:

expr = "R*I-V"
I = symbols("I")
expr = parse_expr(expr, local_dict={"I": I})

expr = expr.subs("I",20)
expr = expr.subs("R",10)

answer = solve(expr,V)
print(answer)
# [200]

huangapple
  • 本文由 发表于 2023年4月7日 00:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定