Is it possible to construct symbols in SymPy that anticommute? - python

I need to implement some Grassmann variables in python (i.e. anti-commuting variables). In other words, I would like something with behavior as follows
>>> from sympy import *
>>> x, y = symbols('x y')
>>> y*x
-x*y
>>> y*y
0
One other feature I would need from this is the ability to give a canonical ordering to my variables. When I typed >>> y*x, it would certainly have been valid to also output y*x over -x*y. But, I would like the ability to choose that x should appear to the left of y (perhaps only after calling a function simplify(y*x)).
Does SymPy or some other library have this ability? If not, what would be the best way to go about implementing this myself (e.g. should I create a symbolic library myself, extend SymPy, etc.)?

You can make a new class inheriting from Symbol and change its behaviour on multiplication (__mul__) to the desired one.
To make this any useful, you need a canonic ordering anyway, which should be the same as SymPy’s (which at a quick glance appears to be by name, i.e., Symbol.name) to avoid problems.
from sympy import Symbol, S
class AnticomSym(Symbol):
def __new__(cls,*args,**kwargs):
return super().__new__(cls,*args,**kwargs,commutative=False)
def __mul__(self,other):
if isinstance(other,AnticomSym):
if other==self:
return S.Zero
elif other.name<self.name:
return -Symbol.__mul__(other,self)
return super().__mul__(other)
def __pow__(self,exponent):
if exponent>=2:
return S.Zero
else:
return super().__pow__(exponent)
x = AnticomSym("x")
y = AnticomSym("y")
assert y*x == -x*y
assert y*y == 0
assert y**2 == 0
assert y**1 == y
assert ((x+y)**2).expand() == 0
assert x*y-y*x == 2*x*y
Now, this still does not resolve complex products such as x*y*x*y correctly.
For this, we can write a function that sorts an arbitrary product (using bubble sort):
from sympy import Mul
def sort_product(product):
while True:
if not isinstance(product,Mul):
return product
arglist = list(product.args)
i = 0
while i < len(arglist)-1:
slice_prod = arglist[i]*arglist[i+1]
is_mul = isinstance(slice_prod,Mul)
arglist[i:i+2] = slice_prod.args if is_mul else [slice_prod]
i += 1
new_product = Mul(*arglist)
if product == new_product:
return new_product
product = new_product
z = AnticomSym("z")
assert sort_product(y*(-x)) == x*y
assert sort_product(x*y*x*y) == 0
assert sort_product(z*y*x) == -x*y*z
Finally, we can write a function that sorts all products within an expression by iterating through the expression tree and applying sort_product to every product it encounters:
def sort_products(expr):
if expr.is_Atom:
return expr
else:
simplified_args = (sort_products(arg) for arg in expr.args)
if isinstance(expr,Mul):
return sort_product(Mul(*simplified_args))
else:
return expr.func(*simplified_args)
from sympy import exp
assert sort_products(exp(y*(-x))) == exp(x*y)
assert sort_products(exp(x*y*x*y)-exp(z*y*z*x)) == 0
assert sort_products(exp(z*y*x)) == exp(-x*y*z)
Note that I may still not have accounted for every eventuality.

Wrzlprmft's answer is a great start so I will add the next logical step. Since you are asking for anticommuting symbols to be handled by a computer algebra system, it is reasonable to assume that you want to be able to differentiate with respect to them. This will require a function to overwrite sympy's product rule.
from sympy import Add, Mul, prod
from sympy.ntheory.multinomial import multinomial_coefficients_iterator
def AnticomDeriv(ptr, s, n):
args = ptr.args
m = len(args)
terms = []
factor = S.One
if isinstance(s, AnticomSym):
if n > 1:
return S.Zero
args = list(args)
for i in range(len(args)):
d = args[i].diff(s)
terms.append(factor * reduce(lambda x, y: x*y, (args[:i] + [d] + args[i + 1:]), S.One))
if isinstance(args[i], AnticomSym):
factor *= -1
return Add.fromiter(terms)
for kvals, c in multinomial_coefficients_iterator(m, n):
p = prod([arg.diff((s, k)) for k, arg in zip(kvals, args)])
terms.append(c * p)
return Add(*terms)
Mul._eval_derivative_n_times = AnticomDeriv
This will give the following (correct) behaviour.
>>> x = AnticomSym('x')
>>> y = AnticomSym('y')
>>> expr = x*y
>>> expr.diff(x)
y
>>> expr.diff(y)
-x

Related

How to find the sum of this series using loops

x - x^2/fact(2) + x^3/fact(3) ... -x^6/fact(6)
I tried various ways, even used nested 'for' loops, but I can't seem to figure out the code, any help?
you could try this; order defines how many terms should be taken into account:
def taylor(x, order=3):
x_n = x
fact = 1
sign = 1
res = 0
for n in range(2, order+2):
res += sign * x_n/fact
x_n *= x
fact *= n
sign = -sign
return res
for comparison (because this is the same function):
from math import exp
def real_funtion(x):
return 1-exp(-x)

New factorial function without 0

So, the standard factorial function in python is defined as:
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x-1)
Since n! := n * (n-1) * ... * 1, we can write n! as (n+1)! / (n+1). Thus 0! = 1 and we wouldn't need that if x == 0. I tried to write that in python to but I didn't work. Can you guys help me?
Since this is a recursive function (return x * factorial(x-1)) you must have an end condition (if x == 0:).
It is true that n! == (n+1)! / (n+1) and you could change your recursive call to :
def factorial(x):
return factorial(x+1) / (x+1)
But that again would have no end condition -> endless recursion (you will calling the next (n+1)! and than the (n+2)! and so on forever (or until you'll get an exception)).
BTW, you can have the condition stop your execution at 1:
if x == 1:
return 1
You wouldn't want to use recursive function for things that are not limited, hence I suggest doing a little bit importing from the standard library
from functools import reduce
import operator
def fact(x):
if not isinstance(x, int) or x <= 0:
raise Exception("math error")
else:
return reduce(operator.mul, range(1, x + 1), 1)
print(fact("string"))
print(fact(-5))
print(fact(0))
print(fact(5))
Just realized that there is no need for a hustle like that:
def fact2(x):
if not isinstance(x, int) or x <= 0:
Exception("math error")
else:
y = 1
while x > 1:
y *= x
x -= 1
return y

Higher order python functions and setting their attributes correctly

I'm currently writing a function that makes and returns a new function to create polynomial expressions. I want the function to store the coefficients of the polynomial and the polynomial itself in string form. However, it doesn't seem that I can set either of the attributes without the interpreter insisting the newly created function has no such attribute.
Please see my function below
def poly(coefs):
"""Return a function that represents the polynomial with these coefficients.
For example, if coefs=(10, 20, 30), return the function of x that computes
'30 * x**2 + 20 * x + 10'. Also store the coefs on the .coefs attribute of
the function, and the str of the formula on the .__name__ attribute.'"""
# your code here (I won't repeat "your code here"; there's one for each function)
def createPoly(x):
formulaParts = []
power = 0
createPoly.coefs = coefs
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power +=1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.value = eval(createPoly.__name__)
return createPoly.value
return createPoly
As you can see when I set the attributes in the above code and use them there is no problem. However if I use code like the below that's when the error occurs
y = poly((5,10,5))
print(y.__name__)
It might be something REALLY simple I'm overlooking. Please help
Your code to set up the inner function can't be inside the inner function:
def poly(coefs):
def createPoly(x):
createPoly.value = eval(createPoly.__name__)
return createPoly.value
formulaParts = []
power = 0
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power += 1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.coefs = coefs
return createPoly

Mathematical Function in Python does not work properly?

def trapezoidal(f, a, b, n):
h = float(b - a) / n
s = 0.0
s = s + f(a)
i=1
while i<=n-1:
s = s + f(a + i*h)
i= i +1
s = s + f(b)
s = s*h
return s
def f(x):
x = float(x)
return (-1/6)*(x-1)*(x-2)*(x+2)*(x-4)
lel = trapezoidal(f, -2, 4, 10)
print ("%.3f" % lel)
ok = f(-0.8)
print ok
I am trying to build a program that calculates integrals using the trapezoid rule. When I do it on paper it works fine but my f function does not work properly. For example f(-0.8) should be equal to 4.8384 but when I run it shows 29.0304. Please help?
If you are using Python 2.x
def f(x):
x = float(x)
return (-1/6)*(x-1)*(x-2)*(x+2)*(x-4)
The first term in your expression is doing integer division. The result of that division will be promoted to float later during the multiplication, but it is too late by then.
>>> (-1/6)
-1
You need to keep everything in floats
def f(x):
x = float(x)
return (-1.0/6.0)*(x-1)*(x-2)*(x+2)*(x-4)
Try
return (-1.0/6)*(x-1)*(x-2)*(x+2)*(x-4)

How to get the logical right binary shift in python

As revealed by the title, in JavaScript there is a specific operator >>>. For example, in JavaScript we will have the following result:
(-1000) >>> 3 = 536870787
(-1000) >> 3 = -125
1000 >>> 3 = 125
1000 >> 3 = 125
So is there a certain method or operator representing this >>>?
There isn't a built-in operator for this, but you can easily simulate the >>> yourself:
>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n
...
>>> rshift(-1000, 3)
536870787
>>> rshift(1000, 3)
125
The following alternative implementation removes the need for the if:
>>> def rshift(val, n): return (val % 0x100000000) >> n
No, there isn't. The right shift in python is arithmetical.
Here's a spinoff of aix's answer. The normal right-shift operator will work if you feed it a positive value, so you're really looking for a conversion from signed to unsigned.
def unsigned32(signed):
return signed % 0x100000000
>>> unsigned32(-1000) >> 3
536870787L
Trying to flip the sign bit of a negative number by masking it with 0x100000000 is fundamentally misconceived as it makes hard assumptions about the word length. In my time as a programmer I have worked with 24-, 48-, 16-, 18-, 32-, 36- and 64-bit numbers. I have also heard of machines that work on odd-lengths, such as 37 and others that use ones-complement, and not twos-complement, arithmetic. Any assumptions you make about the internal representation of numbers, beyond that they are binary, is dangerous.
Even the binary assumption is not absolutely safe, but I think we'll allow that. :)
Numpy provides the right_shift() function that does this:
>>> import numpy
>>> numpy.right_shift(1000, 3)
125
You can do a bitwise shift padding with zeros with the bitstring module using the >>= operator:
>>> a = BitArray(int=-1000, length=32)
>>> a.int
-1000
>>> a >>= 3
>>> a.int
536870787
You need to remember that if the number is negative, the top bit is set and with each shift right you need to make the top bit set as well.
Here is my implementation:
def rshift(val, n):
s = val & 0x80000000
for i in range(0,n):
val >>= 1
val |= s
return val
A solution that works without a modulo:
>>> def rshift(val,n): return (val>>n) & (0x7fffffff>>(n-1))
This works since 7fffffff is a positive number and right shifting that will add zeros to the left.
You could also use floor division:
def rshift(val, n):
if val > 0:
return val >> n
return val // -(2^n)
This is not an efficient approach but works just as expected
def _toBinary(x):
x=int(x)
binary = []
while x > 0:
binary.append(str(x%2))
x=int(x/2)
return "".join(binary[::-1])
def _fromBinary(xs):
ans = 0
for i,x in enumerate(xs[::-1]):
if x == '1':
ans += 2**i
return ans
def leftLogicalShift(x,n=1):
if not type(x) == int:
return x
xs = _toBinary(x)
xs = [x for x in xs]
for _ in range(n):
xs.pop(0)
xs.append('0')
return _fromBinary("".join(xs))
def rightLogicalShift(x,n=1):
if not type(x) == int:
return x
xs = _toBinary(x)
xs = [x for x in xs]
for _ in range(n):
xs.pop()
xs.insert(0,'0')
return _fromBinary("".join(xs))
def leftArithmeticShift(x,n=1):
return leftLogicalShift(x,n)
def rightArithmeticShift(x,n=1):
if not type(x) == int:
return x
xs = _toBinary(x)
xs = [x for x in xs]
for _ in range(n):
tmp = xs[0]
xs.pop()
xs.insert(0,tmp)
return _fromBinary("".join(xs))
lls = leftLogicalShift(10,2)
print(lls) # 8
rls = rightLogicalShift(10,2)
print(rls) # 2
las = leftArithmeticShift(10,2)
print(las) # 8
ras = rightArithmeticShift(10,2)
print(ras) # 14
references:
https://open4tech.com/logical-vs-arithmetic-shift/
https://www.interviewcake.com/concept/java/bit-shift
I think a logical right binary shift is not available in Python directly. Instead you can use Javascript in Python as in:
import js2py
rshift = js2py.eval_js('function $(a, b){ return a >>> b}')
print (rshift(244, 324)) #15
The top-voted answer produces WRONG results for val < 0 and n == 0!
def rshift(val, n):
if (val >= 0): return val >> n
elif (n == 0): return val
else: return (val + 0x10000000) >> n
>>> rshift(-1, 0)
-1

Categories