I'm trying to be able to create symbols for a DH table, but rather that write it out, create a function. However, I don't know how to call the variable when making the table. Here is a synopsis of the problem:
from sympy import *
def naming_symbols(N):
theta = symbols(f"theta:{N}")
L = symbols(f"L:{N}")
alpha=symbols(f"alpha:{N}")
d=symbols(f"d:{N}")
pprint(theta[:])
pprint(L[:])
pprint(alpha[:])
pprint(d[:])
return theta, L, alpha, d
naming_symbols(3)
print(theta2)
returns:
"*FileName*", line 18, in <module>
print(theta2)
NameError: name 'theta2' is not defined
(θ₀, θ₁, θ₂)
(L₀, L₁, L₂)
(α₀, α₁, α₂)
(d₀, d₁, d₂)
Process finished with exit code 1
This is the same for "theta_2" and "theta"
How do I call the created symbols? As in, I want to put "theta2" in the table, but it doesn't recognize it as a created symbol. I think I need to add the symbols into a dictionary or something, but don't know how to do that either. I thought the creation would add it to the dictionary... but... well, please help.
There is a difference between the Symbol (a python object that SymPy creates) and the variable that you assign it to. You already know that you can call a value like 1 anything you want:
>>> x = 1
>>> y = 1
The same is true for a Symbol that you create.
>>> my_x = Symbol('x'); my_x
x
The convention is to often use a variable name that matches the Symbol name, but this is not necessary. Notice that printing my_x
(the variable) shows x
(the Symbol).
The symbols
command creates a tuple of Symbols. You can call that tuple anything you want, just like you can with numerical values
>>> v = (1, 2); v[0]
1
>>> my_v = symbols('v:3'); my_v[0]
v0
Your function is creating tuples of Symbols. You are assigning (and returning) those tuples from the function. In order to use those locally outside of the function you have to create python variable names for the elements of the tuples or you can access them by index just like for the tuple v
defined above:
>>> one, two = v # assigning names for the tuple elements
>>> one
1
>>> v[1] # using index to the tuple name `v`
2
>>> naming_symbols(2)
(θ₀, θ₁)
(L₀, L₁)
(α₀, α₁)
(d₀, d₁)
((theta0, theta1), (L0, L1), (alpha0, alpha1), (d0, d1))
>>> t, l, a, d = _ # assigning names to the tuples
>>> t[0]
theta0
>>> t0,t1 = t # assigning names to the elements of a tuple
>>> t0
theta0
___ probably stop here, but note...
It is possible to create names for the variables locally using the var
command instead of the symbols
command. It is probably better for you to learn how to do the above, but this is an example of using var
:
>>> def do(n):
... var(f'y:{n}')
...
Until this function is run, y0 is not defined
>>> y0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'y0' is not defined
But after running it with n=3, y0, y1 and y2 will exist in the local namespace of python:
>>> do(3)
>>> y0
y0
>>> y1
y1
Note that the naming convention -- calling the SymPy symbol by the matching variable name -- is used. This only works if you create Symbol names that are valid python variables names. So although var('x(1)')
will create a Symbol with the name x(1)
you cannot type this to use the variable:
>>> var('x(1)')
x(1)
>>> x(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Symbol' object is not callable
>>> y=_ # assign the name y to this Symbol
>>> y
x(1)