how to covert code from pine script to python - python

how to convert this type of code from pine script to python
pine script code
get2PoleSSF(src, length) =>
PI = 2 * asin(1)
arg = sqrt(2) * PI / length
a1 = exp(-arg)
b1 = 2 * a1 * cos(arg)
c2 = b1
c3 = -pow(a1, 2)
c1 = 1 - c2 - c3
ssf = 0.0
ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
this is the part im trying to convert it to python
the value of of the code
I tried this
def get2PoleSSD(src,length):
PI = 2* np.arcsin(1)
arg = np.sqrt(2)* PI / length
a1 = np.exp(-arg)
b1 = 2 * a1 *np. cos(arg)
c2 = b1
c3 = -pow(a1, 2)
c1 = 1 - c2 - c3
df['ssf'] = 0.0
df['ssf'] = c1* src
df['ssf_1p'] = df['ssf'].shift(1)
df['ssf_2p'] = df['ssf'].shift(2)
df['ssf_n'] = c1 * src + c2 * df['ssf_1p'] + c3 * df['ssf_2p']
the value from my code
but the value doesn't match at all

Related

Negative values with GCD python program

Iam doing GCD tool in python but for some reason every time I use 2 negative values I get back negative value back. Beceause this is for school i cant use things like obs() ort math etc.. Can some help me with that?
from sys import stdin
a = 0
b = 0
a0 = 0
b0 = 0
a1 = 0
b1 = 0
n = 0
na = 0
nb = 0
q = 0
for line in stdin:
input = line.lstrip().rstrip().split()
if line == '' or len(input) != 2:
break
a, b = [int(x) for x in line.lstrip().rstrip().split()]
if a > b:
a, b = b, a
#
# a | b
# +---------+
# a | 1 | 0 | "( a0, b0 )"
# b | 0 | 1 | "( a1, b1 )"
# n | na | nb | q
# | | |
#
#
a0 = 1
b0 = 0
a1 = 0
b1 = 1
n = a % b
q = a / b
na = a0 - q * a1
nb = b0 - q * b1
a = b
a0 = a1
b0 = b1
b = n
a1 = na
b1 = nb
while n != 0:
n = a % b
q = a // b
na = a0 + q * a1
nb = b0 + q * b1
a = b
a0 = a1
b0 = b1
b = n
a1 = na
b1 = nb
print(a)
I tried messing around with operators. I expect to -888 -2 be 2 not -2 (I need to fix the code not to edit results )
Edit 1 : Here are some examples of what i need
Input Output
7 11 1
888 2 2
905 5 5
-7 11 1
-888 -2 2
905 -5 5

Concatenate the columns of a dataframe (with string values) in python-like paste0 function in R [duplicate]

This question already has answers here:
Combine two columns of text in pandas dataframe
(21 answers)
Closed 1 year ago.
Below is the input data
Type Cat Var Dist Count
#joy A1 + B1 x + y + z 0:25:75 4
.cet C1 + D1 p + q 50:50 2
sam E1 + F1 g 100:3:2 10
Below is the intended output
Type Cat Var Dist Count Output
#joy A1 + B1 x + y + z 0:25:75 4 #joyA1 + B1x + y +z
.cet C1 + D1 p + q 50:50 2 .cetC1 + D1p + q
sam E1 + F1 g 100:3:2 10 samE1 + F1g
Below is the try from my end:
df.iloc[:,0:3].dot(['Type','Cat','Var'])
You can do that using
df['output'] = df['Type'].map(str) + df['Cat'].map(str) + df['Var].map(str)
you can simply use:
df['Output']=df['Type']+' '+df['Cat']+' '+df['Var']
output:
Type Cat Var Dist Count output
0 #joy A1 + B1 x + y + z 0.018229167 4 #joy A1 + B1 x + y + z
1 .cet C1 + D1 p + q 50:50:00 2 .cet C1 + D1 p + q
2 sam E1 + F1 g 100:03:02 10 sam E1 + F1 g
Base R: Using paste0
df$Output <- paste0(df$Type, df$Cat, df$Var)
Type Cat Var Dist Count Output
1 #joy A1 + B1 x + y + z 0:25:75 4 #joy A1 + B1 x + y + z
2 .cet C1 + D1 p + q 50:50 2 .cet C1 + D1 p + q
3 sam E1 + F1 g 100:3:2 10 sam E1 + F1 g
OR
library(dplyr)
df %>%
mutate(Output = paste(Type, Cat, Var, sep = ""))
Type Cat Var Dist Count Output
1 #joy A1 + B1 x + y + z 0:25:75 4 #joy A1 + B1 x + y + z
2 .cet C1 + D1 p + q 50:50 2 .cet C1 + D1 p + q
3 sam E1 + F1 g 100:3:2 10 sam E1 + F1 g
OR:
library(tidyr)
df %>%
unite(Output, c(Type, Cat, Var), remove=FALSE)
Output Type Cat Var Dist Count
1 #joy_A1 + B1_x + y + z #joy A1 + B1 x + y + z 0:25:75 4
2 .cet_C1 + D1_p + q .cet C1 + D1 p + q 50:50 2
3 sam_E1 + F1_g sam E1 + F1 g 100:3:2 10

Python: minimize multiple functions with multiple variables

I am trying to minimize 2 functions with 2 variables at the same time.
I have a set of data and 2 equations like:
B1 = 4
P1 = 6
G1 = 2
E1 = 3
F1 = B - G - E = -1
F2 = P - G - E = 1
Given a new B2 = 5 and P2 = 6 I would like to calculate the variables G2, E2 so that the difference between the old values F1 = -1 and F2 = 1 and the new values F3 and F4 are minimized:
B2 = 5
P2 = 6
G2 = ?
E2 = ?
F3 = B2 - G2 - E2 ---> as close as possible to F1
F4 = P2 - G2 - E2 ---> as close as possible to F2
I was trying:
def diff(param):
G2, E2 = param
return abs(B1 - G2 - E2 - F1)
x0 = [2,2]
res = minimize(diff, x0)
This resolve properly the minimization that gives F3 = F1 = -1, but does not solve my intention to also minimize the difference (F4 - F2).
Do you know how to include also the second minimization problem?
thank you in advance
I am not sure if my approach is too naive (it probably is, to be honest), but considering that you want to minimize two differences, meaning that each difference must tend to zero, then why don't you try to minimize the sum of the differences? This sum should also tend to zero. It would look like that:
from scipy.optimize import minimize
def diff(x):
G2, E2 = x
return (abs(B2 - G2 - E2 - F1 + P2 - G2 - E2 - F2))
B1 = 4
P1 = 6
G1 = 2
E1 = 3
F1 = B1 - G1 - E1
F2 = P1 - G1 - E1
B2 = 5
P2 = 6
res = minimize(diff, x0=(1, 1))
res.x returns [2.75, 2.75], which gives F3 = -0.5, F4 = 0.5.

Solve nonlinear equations with sympy, but i got results with a small imaginary part

I am trying to solve a system of nonlinear equations with Sympy and Python.
The result is almost right, but always with a extremly small imaginary part, and the process is time consuming.
I also try the same computation under Matlab, the result is pretty good and fast.
I know that small imaginary part can be ignored. But I think there must be something wrong in my code which result in slowly and imaginary part. Can any one help me with this?
Python:3.6
Sympy:1.1.1
import sympy
A1, B1, C1, D1, E1, F1 = (0.0019047619047619048,
-1.7494954273533616e-19,
0.0004761904761904762,
-8.747477136766808e-18,
0.047619047619047616,
1.0)
A2, B2, C2, D2, E2, F2 = (8.264462809917356e-05,
-0.0,
0.00033057851239669424,
-0.008264462809917356,
-0.03305785123966942,
1.0)
k, b = sympy.symbols('k b')
eq1 = B1 ** 2 * b ** 2 + 2 * B1 * D1 * b - 2 * B1 * E1 * b * k - 4 * F1 * B1 * k + D1 ** 2 + 2 * D1 * E1 * k + \
4 * C1 * D1 * b * k + E1 ** 2 * k ** 2 - 4 * A1 * E1 * b - 4 * A1 * C1 * b ** 2 - 4 * C1 * F1 * k ** 2 - 4 * A1 * F1
eq2 = B2 ** 2 * b ** 2 + 2 * B2 * D2 * b - 2 * B2 * E2 * b * k - 4 * F2 * B2 * k + D2 ** 2 + 2 * D2 * E2 * k + \
4 * C2 * D2 * b * k + E2 ** 2 * k ** 2 - 4 * A2 * E2 * b - 4 * A2 * C2 * b ** 2 - 4 * C2 * F2 * k ** 2 - 4 * A2 * F2
s=sympy.solve([eq1,eq2],[k,b])
print(s)
That's what I got under Python and Sympy, with an extremely small imaginary part. And it almost takes 10 seconds. That is not acceptable for my whole project.
[(1.07269682322063 + 2.8315655624133e-28*I, -27.3048937553762 + 0.e-27*I),
(1.79271658724978 - 2.83156477591471e-28*I, -76.8585791921325 - 0.e-27*I),
(2.34194482854222 + 2.83156702952074e-28*I, -19.2027508047623 - 0.e-26*I),
(5.20930842765403 - 2.83156580622397e-28*I, -105.800442914396 - 7.59430998293648e-28*I)]
That's what I got under MATLAB with 'solve'. It's pretty fast. That's what I wanted.
k =
5.2093
1.7927
1.0727
2.3419
b =
-105.8
-76.859
-27.305
-19.203
SymPy intends to be a symbolic package, not numeric, so the results should be expected. There is, however, the function nsolve that can be used to find numerical solutions. There is no dedicated method (that I am aware of) that will compute all roots of a polynomial -- in this case, a pair of quadratics -- in SymPy/mpmath. You would have to find roots one by one:
>>> list(nsolve((eq1, eq2), (k,b), (1, 1)))
[1.07269682322063, -27.3048937553762]
But one can use the existing tools to build a solver for such equations. The following is an example (with the potential for lots of numerical issues in corner cases):
def n2solve(eq1, eq2, x, y, n):
"""Return numerical solutions for 2 equations in
x and y where each is polynomial of order 2 or less
as would be true for equations describing geometrical
objects.
Examples
========
>>> n2solve(x**2 + y, y**2 - 3*x*y + 4, x, y, 3)
(-2.82, -7.96)
(-1.34, -1.80)
"""
from sympy.core.containers import Tuple
from sympy.solvers.solvers import unrad, solve
eqs = Tuple(eq1, eq2)
sym = set([x, y])
assert all(i.free_symbols == sym for i in eqs)
anx = solve(eq1, x)[0]
yeq = eq2.subs(x, anx)
z = unrad(yeq)
z = z[0] if z else yeq
yy = real_roots(z)
def norm(x,y):
return abs((x**2+y**2).n(2))
got=[]
for yi in yy:
yi = yi.n(n)
ty = eqs.subs(y, yi)
for xi in real_roots(ty[0]):
xi = xi.n(n)
got.append((norm(*ty.subs(x, xi)), xi, yi))
return sorted([(x,y) for e,x,y in sorted(got)[:len(got)//2]])
This gives the following solutions for the equations posed in the question:
[(1.07, -27.3),
(1.79, -76.9),
(2.34, -19.2),
(5.21, -106.)]

Creat a inverse distance weighting Function in VBA

I want to estimate the inverse distance weighting (IDW) interpolation value of rainfall.
I identified the nearest three stations to the needed station to obtain the interpolated value.
I have the following table:
So, the equation of IDW:
[(Station_valu1/Dist1^2)+(Station_valu2/Dist2^2)+(Station_valu3/Dist3^2)]
—————————————————————————————————————————————————————————————————————————
1/Dist1^2 + 1/Dist2^2 + 1/Dist3^2
There would be several situations trying to address them in this equation:
1) If Value1 or Dist1 are empty and Value2, Dist2, Value3, Dist3 are not
then, disregard Value1, Dist1 from the equation, and consider only Value2, Dist2, and Value3, Dist3.
Result of IDW will be:
[Station_valu2/Dist2^2)+(Station_valu3/Dist3^2)]
————————————————————————————————————————————————
Dist2^2 + 1/Dist3^2
We will have the same scenario with Value2, Dist2, and Value3, Dist3 if anyone of them has null value.
I came up with this code:
Function IDWW(Value1, Value2, Value3, Dist1,Dist2,Dist3)
Dim a1 As Variant
Dim b1 As Variant
Dim a2 As Variant
Dim b2 As Variant
Dim a3 As Variant
Dim b3 As Variant
If Value1 <> "" And Dist1 <> "" Then
a1 = Value1 / (Dist1) ^ 2
b1 = 1 / (Dist1) ^ 2
ElseIF Value1 = "" OR Dist1 = "" Then
a1 = ""
b1 = ""
End If
If Value2 <> "" And Dist2 <> "" Then
a2 = Value2 / (Dist2) ^ 2
b2 = 1 / (Dist2) ^ 2
ElseIF Value1 = "" OR Dist1 = "" Then
a2 = ""
b2 = ""
End If
If Value3 <> "" And Dist3 <> "" Then
a3 = Value3 / (Dist3) ^ 2
b3 = 1 / (Dist3) ^ 2
ElseIF Value3 = "" OR Dist3 = "" Then
a3 = ""
b3 = ""
End If
IDWW = (a1+a2+a3) / (b1+b2+b3)
End Function
Please, I need your help to solve this issue!
As far as I can see, all you want to do is set values to be zero if they are empty:
Function IDWW(Value1, Value2, Value3, Dist1,Dist2,Dist3)
Dim a1 As Variant
Dim b1 As Variant
Dim a2 As Variant
Dim b2 As Variant
Dim a3 As Variant
Dim b3 As Variant
If Value1 <> "" And Dist1 <> "" Then
a1 = Value1 / (Dist1) ^ 2
b1 = 1 / (Dist1) ^ 2
Else
a1 = 0
b1 = 0
End If
If Value2 <> "" And Dist2 <> "" Then
a2 = Value2 / (Dist2) ^ 2
b2 = 1 / (Dist2) ^ 2
Else
a2 = 0
b2 = 0
End If
If Value3 <> "" And Dist3 <> "" Then
a3 = Value3 / (Dist3) ^ 2
b3 = 1 / (Dist3) ^ 2
Else
a3 = 0
b3 = 0
End If
'Avoid a problem if all 3 distances are empty
If b1 + b2 + b3 = 0 Then
IDWW = 0
Else
IDWW = (a1+a2+a3) / (b1+b2+b3)
End If
End Function

Categories

Resources