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])
Related
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
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 would like to calculate this sum
using numpy.sum - how can I do that?
Phi is a function that takes one parameter, y and x are vectors of length i
Start with x and y as numpy arrays
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
You will pass in the array,
r = y - (theta_0*x + theta_1)
Define your function, and hopefully you can do vectorized operations within the function, but whatever, the function needs to return a numpy vector if you want to use numpy.sum
def Phi(r):
a = r*r # and example operation
return a # returning a numpy array
Then call the function and sum:
R = Phi(r).sum()
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 a array, where i would like to multiply the elements inside the array by themselves (product) and i have the following vector to be multiplied by the input vector: test_vector = array([0.1, 0.3, 0.4, 0.5, 0.6).
I am looking for an easy way to automate this task
NumPy solution:
import numpy as np
arr_1 = np.array([1, 0, 1, 0, 1])
arr_2 = np.array([0.42, 0.53, 0.62, 0.60, 0.69])
res = np.prod(np.where(arr_1, arr_2, 1 - arr_2))
print(res)
Output:
0.033779088
There might be a more efficient way.
I'm not quite sure what you're calling that first array. I'm calling it signs.
result = 1
for sign, value in zip(signs, test_vector):
result *= (value if sign == 0 else 1 - value)
If these vectors are long rather than short examples as given here, you might want to switch to using numpy.
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 5 years ago.
Improve this question
sparse
array objects will have a fixed size n set when they are created - attempting to set or get elements larger
than the size of the array should raise an IndexError
Use Scipy sparse Matrics e.g. COO sparse matrix
matrix = sparse.coo_matrix((C,(A,B)),shape=(5,5))
Or you can use Pandas sparseArray :
arr = np.random.randn(10)
arr[2:5] = np.nan; arr[7:8] = np.nan
sparr = pd.SparseArray(arr)
I'd bet these are already implemented in numpy or scipy.
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 6 years ago.
Improve this question
I have the below function in Python which I need to convert into R
def hamming(h1, h2):
h, d = 0, h1 ^ h2
while d:
h += 1
d &= d - 1
return h
But I don't know how to handle the bitwise piece.
UPDATE:
I had to update the question since I did a mistake of not sharing what I had done.
I know about the BitWise operator but I was not getting the same answer.
I should have included my code which would have not created all these confusion.
My apologies for not been precise with the question.
I had written the function as below:
hamming <- function(h1, h2) {
h <- 0
d <- h1^h2
while (d) {
h <- h + 1
d = bitwAnd(d, d-1)
}
return(h)
}
But I seem to get different results from both the function. Don't know which line is wrong.
UPDATE: I took ^ in Python to be same in R and I was wrong. I figured out the issue.
Thanks everyone
There is a set of bitwise functions in base R. See ?bitwAnd for the one you are looking for and others available.