Consider two sets i,j which both have m elements. Say we have an expression which describes a sum of terms. Each term can be described as a product of an element of i and j. Now, I would like to sum over each element of j, where each element has the range [i1,i2,...,im].
In the context of python & sympy, this is difficult since sympy's Sum describes the summation variable with (symbol,start,stop), which assume integer steps.
To demonstrate what I mean, consider the following code:
>>> from sympy import *
>>> i = symbols('i1,i2,i3,i4') # for the case m = 4
>>> j = symbols('j1,j2,j3,j4')
Here I use permutations to setup the expression:
>>> from itertools import permutations as perm
>>> c = list(perm(range(4),2))
>>> a,b = c[0]
>>> expr = i[a]*j[b]
>>> for a,b in c[1:]:
>>> expr += i[a]*j[b]
>>> print(expr)
i1*j2 + i1*j3 + i1*j4 + i2*j1 + i2*j3 + i2*j4 + i3*j1 + i3*j2 + i3*j4 + i4*j1 + i4*j2 + i4*j3
Now, using Sum over each j with range of i. It would be ideal if I could write one of the following:
>>> s = Sum(expr,(j,i))
>>> s = Sum(expr,(j1,i),(j2,i),...,(jm,i))
But that's not canonical with the sympy documentation. Are there any other methods which can be used to solve this problem?
Edit:
In this post, I tried to isolate the problem by only using elements i,j in expr. The full context problem is where expr is a sum of Kronecker Delta functions of i,j and using a sum over index set j, where each element of j has range i. For example:
>>> from sympy import KroneckerDelta as KD
>>> expr = KD(i[0],j[1]) # Only doing j[1] to reduce clutter
>>> print(expr)
KroneckerDelta(i1,j2)
>>> s = Sum(expr,(j[1],i)).doit()
>>> print(s)
# Desired output to look like:
1 + KroneckerDelta(i1,i2) + KroneckerDelta(i1,i3) + KroneckerDelta(i1,i4)
This is the reason for which I phrased my question as: summing over each element of j with range i.
IndexedBase can act as a symbolic, integer-indexed array:
>>> from sympy import *
>>> from sympy.abc import k,l
>>> i,j = map(IndexedBase,'ij')
>>> Sum(i[k]*j[l],(k,1,2),(l,1,2)).doit().expand()
i[1]*j[1] + i[1]*j[2] + i[2]*j[1] + i[2]*j[2]
>>> Sum(Piecewise((i[k]*j[l],Ne(k,l)),(0,True)),(k,1,2),(l,1,2)).doit()
i[1]*j[2] + i[2]*j[1]
It's not clear whether you want the cross terms when the indicices are the same, so both versions are shown.
My program:
j = complex(0,1)
G1 = math.exp(j)
That is what I get:
TypeError: can't convert complex to float
What is the best way to solve this?
You can use cmath instead of math:
import cmath
j = complex(0,1)
G1 = cmath.exp(j)
#OmG's way is probably the preferred way, but ** also gives the same answer:
>>> j = complex(0,1)
>>> math.e ** j
(0.5403023058681398+0.8414709848078965j)
In Charm Crypto, how would I go about getting at the multiplicative inverse for ZR? I have roughly the following code:
a = group.random(G)
e = group.random(ZR)
x = a ** e
somestuff()
y = x ** (1/e)
where a is not stored on purpose. However while -e works fine to get the additive inverse there doesn't seem to be a proper way to get at the multiplicative inverse.
Not sure what you mean. 1/e is the proper modular inverse in Charm Crypto. Here is a full example:
>>> from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
>>> group = PairingGroup('SS512')
>>> a = group.random(G1)
>>> a
[2580989876233721415297389560556166670922761116088625446257120303747454767083854114997254567159052287206977413471899062293779511058710074633103823400659019, 5996565379972917992663126989138580820515927146496218666993731728783513412956887506732385903379922348877197471677004946545491932261438787373567446770237791]
>>> e = group.random(ZR)
>>> x = a ** e
>>> x
[6891729780372399189041525470592995101919015470165150216677136432042436097937961533731911650601678002293909918119625724503886943879739773465990776556262311, 1548281541526614042816533932120191809063134798488215929407179466331621937371141709171095414449680510602430538669648224266688052566354236898986673964076468]
>>> y = x ** (1/e)
>>> y
[2580989876233721415297389560556166670922761116088625446257120303747454767083854114997254567159052287206977413471899062293779511058710074633103823400659019, 5996565379972917992663126989138580820515927146496218666993731728783513412956887506732385903379922348877197471677004946545491932261438787373567446770237791]
>>> y == a
True
Perhaps somestuff() changes x or e in some way that it doesn't work.
Is it possible to define an equation and solve a variable in that equation?
D_PWM, Rsense, A = symbols('D_PWM, Rsense, A')
i_out = D_PWM * (A/Rsense)
print i_out
solve(i_out, Rsense)
Result:
A*D_PWM/Rsense
[]
i_out has not been declared as a symbol.
>>> from sympy import *
>>> var('D_PWM, Rsense, A i_out')
(D_PWM, Rsense, A, i_out)
>>> eqn=Eq(i_out,D_PWM * (A/Rsense))
>>> solve(eqn,Rsense)
[A*D_PWM/i_out]
I was wondering if it is somehow possible to set the domain of a math. function. For example, when I define the following expression
>>> import sympy as syp
>>> x = syp.Symbol('x')
>>> miles_to_km = x * 1.609344
>>> miles_to_km.evalf()
1.609344*x
Is it possible to limit the domain so that x is in the range [0, inf)? So the goal is that I could then use the sympy.plot function that produces a graph that starts at 0 and only includes positive x-values in contrast to
If we check the manual by doing:
help(syp.plot)
You will get:
...
expr : Expression representing the function of single variable
range: (x, 0, 5), A 3-tuple denoting the range of the free variable.
...
So, you can:
>>> import sympy as syp
>>> x = syp.Symbol('x')
>>> miles_to_km = x * 1.609344
>>> syp.plot(miles_to_km, (x,0,10))
which will give you the following output: