How to implement this symbolic summation in python? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to implement this summation, any help, please?
enter image description here

Considering that x is a list of numbers, this can be written as following:
H = '+'.join([f'x{i}*x{i+1}' for i in range(1, 21)])
>>>print(H)
'x1*x2+x2*x3+x3*x4+x4*x5+x5*x6+x6*x7+x7*x8+x8*x9+x9*x10+x10*x11+x11*x12+x12*x13+x13*x14+x14*x15+x15*x16+x16*x17+x17*x18+x18*x19+x19*x20+x20*x21'

You need a computer algebra system (CAS). There are a number of software packages in Python that implements CAS, for example, using sympy:
from sympy import *
i = symbols('i')
x = IndexedBase('x')
H = Sum(x[i] * x[i-1], (i, 1, 20))
This will produce H, a symbolic expression the represents the equation in your image.
Or, you can use summation instead to evaluate the Sum into a series of additions:
H_evaluated = summation(x[i] * x[i-1], (i, 1, 20))
or call the doit() function:
H_evaluated == H.doit()
You can try sympy online on https://live.sympy.org/ to play with sympy without installing it locally. Try this equation live

Related

How to separe mathematical operation from math expression like: 'log(2*x)*cos(x)'? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I would like to separate this math expression 'log(2*x)*cos(x)' into 'log(2*x)' and 'cos(x)'. Im using Sympy to solve each part of the expression. I tried regex and ast.parse to separate math operation by parts but I didn't succeeded.
What I'm trying to do is to solve 'log(2*x)' first, 'cos(x)' second and then 'log(2*x)*cos(x)'. How can I get each math operation from an math expression?
You really shouldn't be doing this using regular expressions. You should be letting sympy parse it, and sympy separate it into terms.
>>> expr = log(2 * x) * cos(x)
>>> expr.as_ordered_factors()
[log(2*x), cos(x)]
Most of SymPy objects exposes the args attribute: it returns the arguments of an operator or a function. For example:
from sympy import *
var("x")
expr = log(2*x)*cos(x)
print(expr.args)
# out: (cos(x), log(2*x))
Here, your expression is a multiplication and args returned its factors.
Let's consider an addition:
expr = log(2*x) + cos(x)
print(expr.args)
# out: (cos(x), log(2*x))
Let's now consider a function:
expr = log(2*x)
print(expr.args)
# out: (2*x,)

python coding from matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this code in MATLAB and I am trying to convert it in Python.
A=[1,-0.75,0.25]
yc(1:45)=-2;
y(1:6)=0
u(1:6)=0
[lig,col]=size(A);
alpha(1)=1;
alpha(2)=A(2)-1;
if(col>2)
for i=3:col
alpha(i)=A(i)-A(i-1);
end ;
end;
alpha(col+1)=-A(col);
I don't know how to convert it in python thnx for helping me
It would be better if your code would have been a minimal example of what you are trying to do. You are defining variables that are not even used. But here's a more or less literal translation. Note that you probably want to preallocate alpha (both in Matlab and Python)
import numpy as np
A = np.array([1.0, -.75, .25])
yc = -2 * np.ones(45)
y = np.zeros(6)
u = np.zeros(6)
col = A.size
alpha = np.array([1, A[1] - 1])
if col > 2:
for i in range(2, col):
alpha = np.append(alpha, A[i] - A[i-1])
alpha = np.append(alpha, -A[col-1])

Can we generate random integers basing on Mean and SD in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have already generated data using np.random.normal but I am getting float values. I need to get Integers instead.
Could anyone help me out?
In order to get integers instead of float, you can try using int() to cast the value you are getting. Assuming you want a normal distribution of a thousand samples between -100 and 100, with a mean of 0 and standard deviation of 1, you'd try this:
y = [int(x * 100) for x in np.random.normal(0, 1, 100)]
Here variable y holds the normal integer distribution you want.

Sine square in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Need help. How I can get sine square in python? Is there specific method or function?
Some supposedly obvious solutions are NOT suitable. Examples:
import numpy as np
import math
x = np.arange(0, 3, 0.5)
print([(math.sin(i) ** 2) for i in x])
print([math.sin(math.sin(i))for i in x])
# [0.0, 0.22984884706593015, 0.7080734182735712, 0.9949962483002227, 0.826821810431806, 0.3581689072683868]
#[0.0, 0.4612695550331807, 0.7456241416655579, 0.8401148815567654, 0.7890723435728884, 0.5633808209655248]
# or
x = np.arange(0, 3, 0.5)
print(np.sin(x) ** 2)
print(np.sin(np.sin(x)))
# [0. 0.22984885 0.70807342 0.99499625 0.82682181 0.35816891]
# [0. 0.46126956 0.74562414 0.84011488 0.78907234 0.56338082]
You need to look for math module in Python. See this.
math.sin(x) ** 2
You can also use math.pow(x,y). See this for how to raise a number x raised to the power y.
A small example program.
import math
rad = int(input("Enter radians: "))
print(math.sin(rad) ** 2)
If you want to convert from radians to degrees or vice-versa, have a look at this.
You will be safer if you use it as follows -
((math.sin(x)) ** 2)
Emphasis is to put the math.sin(x) inside bracket.

Generating all possible n*n binary matrix in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm playing with graphs and python, and I'm trying to test some code on all the possible square matrix that represent an adjacency matrix (i.e. matrix with 0 and 1).
We know that there are 2^{n^2} possible matrix of nxn.
What's the best code for generating all the possible n x n binary matrix in python?
I think you can more efficiently compute your results by using mathematical operations with numpy rather than string operations. Try:
shift = np.arange(n*n).reshape(n, n)
for j in range(2**(n*n)):
yield j >> shift & 1
You might be able to use numpy to parallelize the j loop as well, but that might use a lot more memory than the current generator version.
Since I could't find anywhere the solution, and I think it could be helpful to spare some minutes to other people..
def generateAllBinaryMatrix(n):
G = np.zeros([n,n])
cordx=[]
cordy=[]
for x in range(0,n):
for y in range(0,n):
cordx.append(x)
cordy.append(y)
cx=np.array(cordx)
cy=np.array(cordy)
indices=(cx,cy)
print indices
raw_input()
for j in range(0,2**(indices[0].size)):
G[indices] = [1 if digit=='1' else 0 for digit in bin(j)[2:].zfill(indices[0].size)]
yield (G)

Categories