Ask Your Question
1

product of functions

asked 2020-04-12 20:17:08 -0500

vaskunito gravatar image

updated 2020-04-16 02:03:20 -0500

slelievre gravatar image

I just started using SageMath and I am trying to do something simple but I cannot figure it out.

I have a Gaussian function

mu, sigma, x, N = var('mu, sigma, x, N')
P(x) = 1/sqrt((2*pi*sigma^2))*exp(-(x-mu)^2/(2*sigma^2))

and I want to calculate the product of N such functions.

I tried

f = lambda x, N: prod([P for x in range(1, N)])
f(x, N)

and I got the error:

TypeError: unable to convert N to an integer

I works if I choose N to be some number, but I would like to get a symbolic expression output.

edit retag flag offensive close merge delete

Comments

A symbolic product in SageMath is currently not implemented, but you could take a logarithm and use a symbolic sum.

rburing gravatar imagerburing ( 2020-04-13 08:04:14 -0500 )edit
1

In fact it is! I opened

to make this more discoverable.

slelievre gravatar imageslelievre ( 2020-04-16 02:04:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-04-16 00:09:36 -0500

slelievre gravatar image

updated 2020-04-16 00:12:38 -0500

In Sage, sum serves both for Python sums and for symbolic sums, but for products we have prod and product.

Declare an extra symbolic variable k, and instead of

f = lambda x, N: prod([P for x in range(1, N)])

use

f = lambda x, N: product(P, k, 1, N)

Also, check your expression for P. Maybe you want this instead:

P(x) = 1/sqrt((2*pi*sigma^2))*exp(-((x-mu)/sigma)^2/2)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-04-12 20:17:08 -0500

Seen: 55 times

Last updated: Apr 16