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)
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
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
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)
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