0

I am trying to define several functions on, let say, one variable. First I defined x variable:

from sympy import *
x=var('x')

I want to define series of functions using Lambda something look like this:

f0=Lambda(x,x)
f1=Lambda(x,x**2)
....
fn=....

How can I define this? Thanks

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
m.akbari
  • 572
  • 8
  • 22
  • Don't really know why would you want to do such a thing. But, this might help (NOT PYTHONIC) - http://stackoverflow.com/a/4010856/2689986 – shad0w_wa1k3r Nov 02 '13 at 22:19
  • I want to define several random linear functions with N variables. – m.akbari Nov 02 '13 at 22:51
  • Then your question should state that plain and clearly with examples of what the random functions look like. Otherwise no one can answer your question as such. – joojaa Nov 03 '13 at 08:55

1 Answers1

1

It took me a while to understand what your after. It seems that you are most likely after loops, but do not articulate this. So I would suggest you do this:

from sympy import *
x = symbols('x')

f=[]
for i in range(1,11): # generate 1 to 10
    f.append( Lambda(x,x**i) )

# then you use them like this
print( f[0](2) ) # indexes 0 based 
print( f[1](2) ) 
# ,,, up to [9]

Anyway your not really clear in your question on what the progression should be.

EDIT: As for generating random functions here is one that example generates a polynomial with growing order and a random set of lower orders:

from random import randint
from sympy import *
x = symbols('x')

f=[]
for i in range(1,11):
    fun = x**i
    for j in range(i):
        fun += randint(0,1)* x**j
    f.append( Lambda(x,fun) )

# then you use them like this 
# note I am using python 2.7 if you use 3.x modify to suit your need
print( map(str, f) ) #show them all

def call(me):
    return me(2)

print( map(call, f) ) 

Your random procedure may be different as there are a infinite number of randoms available. Note its different each time you run the creation loop it, use random seed to fix the random if needed same generation between runs. The functions once created are stable in one process.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
joojaa
  • 4,354
  • 1
  • 27
  • 45
  • Thanks for replying, I want to define several random linear functions with N variables. I need a fast way to generate functions. Is it possible to use % operator? I mean, I can define variables like x=var('x0:%d' %num_var) where num_var is an integer. – m.akbari Nov 02 '13 at 22:46
  • No your not operating on strings, your operating on objects. In this case i is not a variable it is actually a integer as far as sympy is concerned. However you can do the same thing more compactly with object notation and do any kind of function. But you did not ask this so it impossible to answer, dont expand questions in comments just clarify. You should either modify your question or open a new one, as this is not in your original goal. Please do not use exec there's really no reason to take that speed hit and risk of screwing up. Besides you can think of list notations as index operators. – joojaa Nov 03 '13 at 08:51
  • 1
    It's better to use `symbols` than `var`. – asmeurer Nov 05 '13 at 00:55
  • There is also a function in SymPy called `random_poly` if all you want is a random polynomial. – asmeurer Nov 05 '13 at 00:56
  • @asmeurer not really super knowledgeable about sympy, good to know. Just extending the answer a bit to fit the OP's needs quickly. – joojaa Nov 06 '13 at 18:22