Ask Your Question
1

Multiply symbolic function by number or expression

asked 3 years ago

vejtics gravatar image

I want to define a symbolic function in terms of other symbolic functions, like so:

function('psi psi_1 psi_2')
psi = 1/sqrt(2) * (psi_1 + psi_2 * i)

When running the code, I get the error:

TypeError: unsupported operand type(s) for *: 'NewSymbolicFunction' and 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_gaussian'

The same error appears when I replace i with a real number.

Is there any fundamental mistake I'm making? If so, what should I do instead? If not, is there a workaround?

The reason I am defining a function like this is because I need to find the derivative of the psi function in terms of derivatives of the latter two functions.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 3 years ago

tmonteil gravatar image

updated 3 years ago

psi_1 and psi_2 are kind of not completely defined (in particular, they are not member of the symbolic ring, hence they can not benefit from the coercions between the symbolic ring and other rings), for example at this stage it is not clear how many symbolic variable do they use, see:

sage: parent(psi_1)
<class 'sage.symbolic.function_factory.function_factory.<locals>.NewSymbolicFunction'>

sage: psi_1(x)
psi_1(x)
sage: var('y')
y
sage: psi_1(x,y)
psi_1(x, y)

sage: parent(psi_1(x))
Symbolic Ring

If you decide that those are function of a single variable, it works:

sage: psi = 1/sqrt(2) * (psi_1(x) + psi_2(x) * i)
sage: psi
1/2*sqrt(2)*(psi_1(x) + I*psi_2(x))
Preview: (hide)
link
1

answered 3 years ago

slelievre gravatar image

updated 3 years ago

No need to include psi in your initial function declaration, as you then define psi in the next line.

Not sure how to explain the error you get, but here is something that works:

sage: psi1, psi2 = function('psi_1, psi_2')
sage: psi(t) = 1/sqrt(2) * (psi_1(t) + psi_2(t) * i)

That defines psi as a function. Observe the result:

sage: psi
t |--> 1/2*sqrt(2)*(psi_1(t) + I*psi_2(t))

That function can be differentiated:

sage: diff(psi)
t |--> 1/2*sqrt(2)*(diff(psi_1(t), t) + I*diff(psi_2(t), t))

sage: diff(psi(t), t)
1/2*sqrt(2)*(diff(psi_1(t), t) + I*diff(psi_2(t), t))
Preview: (hide)
link

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: 3 years ago

Seen: 468 times

Last updated: Nov 20 '21