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

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

python sympy cannot sub value when using letter I

问题

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

  1. from sympy import *
  2. V = symbols("V")
  3. I = symbols("I")
  4. R = symbols("R")
  5. expr = "R*I-V"
  6. expr = parse_expr(expr)
  7. expr = expr.subs("I", 20)
  8. expr = expr.subs("R", 10)
  9. answer = solve(expr, V)
  10. 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:

  1. V = symbols("V")
  2. i = symbols("i")
  3. R = symbols("R")
  4. expr = "R*i-V"
  5. expr = parse_expr(expr)
  6. expr = expr.subs("i", 20)
  7. expr = expr.subs("R", 10)
  8. answer = solve(expr, V)
  9. 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.

  1. from sympy import *
  2. V = symbols("V")
  3. I = symbols("I")
  4. R = symbols("R")
  5. expr = "R*I-V"
  6. expr = parse_expr(expr)
  7. expr = expr.subs("I",20)
  8. expr = expr.subs("R",10)
  9. answer = solve(expr,V)
  10. 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:

  1. V = symbols("V")
  2. i = symbols("i")
  3. R = symbols("R")
  4. expr = "R*i-V"
  5. expr = parse_expr(expr)
  6. expr = expr.subs("i",20)
  7. expr = expr.subs("R",10)
  8. answer = solve(expr,V)
  9. 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 字典,将您的符号关联到字符串中的特定字符。例如:

  1. expr = "R*I-V"
  2. I = symbols("I")
  3. expr = parse_expr(expr, local_dict={"I": I})
  4. expr = expr.subs("I", 20)
  5. expr = expr.subs("R", 10)
  6. answer = solve(expr, V)
  7. print(answer)
  8. # [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:

  1. expr = "R*I-V"
  2. I = symbols("I")
  3. expr = parse_expr(expr, local_dict={"I": I})
  4. expr = expr.subs("I",20)
  5. expr = expr.subs("R",10)
  6. answer = solve(expr,V)
  7. print(answer)
  8. # [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:

确定