Sine square 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 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.

Related

List Comprehension starting at a specific number [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 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]

Smart rounding an array 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 12 months ago.
Improve this question
I'd like to make a function that rounds integers or floats homogeneously and smart. For example, if I have an array like:
[0.672, 0.678, 0.672]
my output would be:
[0.67, 0.68, 0.67]
but also if I have this kind of input:
[17836.982, 160293.673, 103974.287]
my output would be:
[17836, 160293, 103974]
But at the same time, if my array only has close together values such as:
[17836.987, 17836.976, 17836.953]
The output would be:
[17836.99, 17836.98, 17836.95]
An automated way could be to compute all absolute differences, getting the min and finding out the number of decimal places to keep to maintain a representative difference.
This doesn't give the exact output you want but follows the general logic.
Here using numpy to help on the computation, the algorithm is O(n**2):
def auto_round(l, round_int_part=False):
import numpy as np
a = np.array(l)
b = abs(a-a[:,None])
np.fill_diagonal(b, float('inf'))
n = int(np.ceil(-np.log10(b.min())))
# print(f'rounding to {n} decimals') # uncomment to get info
if n<0:
if not round_int_part:
return a.astype(int).tolist()
return np.round(a, decimals=n).astype(int).tolist()
return np.round(a, decimals=n).tolist()
auto_round([17836.987, 17836.976, 17836.953])
# [17836.99, 17836.98, 17836.95]
auto_round([0.6726, 0.6785, 0.6723])
# [0.6726, 0.6785, 0.6723]
auto_round([17836.982, 160293.673, 103974.287])
# [ 17836, 160293, 103974]
auto_round([17836.982, 160293.673, 103974.287], round_int_part=True)
# [20000, 160000, 100000]

How to implement this symbolic summation 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 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

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

Python: Derivative of trig functions [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 4 years ago.
Improve this question
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x) evaluated at 5 (radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x)), which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5)).
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366

Categories