Expected a right parenthesis in expression - fortran

xic = ac * x**2.D0 * ( (1.D0 / 3.D0) * (1.D0 - x) *
(1.D0 + 10.D0 * x + x** 2.D0) + 2.D0 * x *
(1.D0 - x) * Log(x) )
I was compiling the above code with fortran and got one error
Expected a right parenthesis in expression at (1)
what should i do?

You are missing line continuation characters. They differ slightly for free and fixed form Fortran. For free form, you need to use & at the end of the line:
xic = ac * x**2.D0 * ( (1.D0 / 3.D0) * (1.D0 - x) * &
(1.D0 + 10.D0 * x + x** 2.D0) &
+ 2.D0 * x * (1.D0 - x) * Log(x) )
For fixed-format this can be done by e.g. & at the sixth column of the following line:
xic = ac * x**2.D0 * ( (1.D0 / 3.D0) * (1.D0 - x) *
& (1.D0 + 10.D0 * x + x** 2.D0)
& + 2.D0 * x * (1.D0 - x) * Log(x) )
Alternatively, you can extend the maximum allowed characters by using (gfortran) -ffree-line-length-0 or -ffixed-line-length-0.

Check the following methods to cut a long line in Fortran :
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap01/continue.html

Related

C++: Get value of variable after performing divide and assignment operator:

I am trying to get a value of a variable "pesoil" before its converted to to mm month^-1
pesoil = beta * rnsoil + CPAIR * RHOAIR * exp_h * vpdo / r_as;
pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2
pesoil *= (etimes * ndays[dm]) / LAMBDA; // convert to mm month^-1
can I do it like this? or will this mess up value stored in "pesoil"
epotential_W = (pesoil /= (beta + PSY * exp_h * (1.0 + r_ss / r_as))); // W m^-2

how can I tell SymPy that some terms are positive then make SymPy simplify `sqrt(x^2)` to `x` where `x` may be a complicated term?

I'm solving the problem is in section Auxiliary Elements in Polya's How to Solve It:
Construct a triangle, being given one angle (A), the altitude (h) drawn from the vertex of the given angle, and the perimeter (p) of the triangle.
I get the solution below, and try to verify it by SymPy:
from sympy import *
h, p, A = symbols("h p A", positive=True)
u = sqrt(h * p * p / 2 / (h + h * cos(A) + p * sin(A)))
v = (p - u * u * sin(A) / h) / 2
y = v + sqrt(v * v - u * u)
z = v - sqrt(v * v - u * u)
x = sqrt(y * y + z * z - 2 * y * z * cos(A))
# x, y, z are solutions
# now verify them:
print("x + y + z =", simplify(x + y + z))
The expected result should be x + y + z = p but the actual output is a bit complicated:
x + y + z = (-p**2*sin(A)/2 + (4*p + 2*sqrt(p**4*sin(A)**2/(h*cos(A) + h + p*sin(A))**2))*(h*cos(A) + h + p*sin(A))/4)/(h*cos(A) + h + p*sin(A))
Here I notice that sqrt(p**4*sin(A)**2/(h*cos(A) + h + p*sin(A))**2) are not simplified to p**2*sin(A)/(h*cos(A) + h + p*sin(A)) because SymPy doesn't know whether sin(A) and h*cos(A) + h + p*sin(A) are positive.
How could I set some parameter to simplify() method to make SymPy do the reasonable simplification with some condition (e.g. some terms are positive so that sqrt() can be simplified)?
I refer Sympy - Simplify expression within domain but it doesn't seem to work.
One way to do this would be to make positive symbols for sin(A) and cos(A) e.g.:
sinA, cosA = symbols('sinA, cosA', positive=True)
You could use those while simplifying and substitute for them after.
Another way that will work for some cases is to use refine e.g.:
In [8]: res = simplify(x + y + z)
In [9]: res
Out[9]:
⎛ ⎛ │ sin(A) │ ⎞ ⎞
p⋅⎜-p⋅sin(A) + ⎜p⋅│───────────────────────│ + 2⎟⋅(h⋅cos(A) + h + p⋅sin(A))⎟
⎝ ⎝ │h⋅cos(A) + h + p⋅sin(A)│ ⎠ ⎠
───────────────────────────────────────────────────────────────────────────
2⋅(h⋅cos(A) + h + p⋅sin(A))
In [10]: refine(res, Q.positive(sin(A)) & Q.positive(cos(A)))
Out[10]:
⎛ ⎛ p⋅sin(A) ⎞ ⎞
p⋅⎜-p⋅sin(A) + ⎜─────────────────────── + 2⎟⋅(h⋅cos(A) + h + p⋅sin(A))⎟
⎝ ⎝h⋅cos(A) + h + p⋅sin(A) ⎠ ⎠
───────────────────────────────────────────────────────────────────────
2⋅(h⋅cos(A) + h + p⋅sin(A))

Argmax function in C++?

I am trying to make armgax function in C++
For example,
C = wage * hour * shock + t + a * R + (1 - delta) * d - d_f - a_f - fx1 * (1 - delta) * d;
double result;
result = (1 / (1 - sig)) * pow(pow(C, psi) * pow(d, 1 - psi), (1 - sig));
Suppose every variable except for 'd_f' and 'a_f' are given and both 'd_f' and 'a_f' some number(double) in [0, 24].
If I want to get the combination of 'd_f' and 'a_f' that maximizes 'result', is there a proper function that I can use?
Thanks.

Which is more costly for the processor?

I am working on a small Math lib for 3D graphics.
I'm not sure what costs more to the CPU/GPU in terms of time.
Right now I am using this to multiply matrix (4x4)
tmpM.p[0][0] = matA.p[0][0] * matB.p[0][0] + matA.p[0][1] * matB.p[1][0] + matA.p[0][2] * matB.p[2][0] + matA.p[0][3] * matB.p[3][0];
tmpM.p[0][1] = matA.p[0][0] * matB.p[0][1] + matA.p[0][1] * matB.p[1][1] + matA.p[0][2] * matB.p[2][1] + matA.p[0][3] * matB.p[3][1];
tmpM.p[0][2] = matA.p[0][0] * matB.p[0][2] + matA.p[0][1] * matB.p[1][2] + matA.p[0][2] * matB.p[2][2] + matA.p[0][3] * matB.p[3][2];
tmpM.p[0][3] = matA.p[0][0] * matB.p[0][3] + matA.p[0][1] * matB.p[1][3] + matA.p[0][2] * matB.p[2][3] + matA.p[0][3] * matB.p[3][3];
tmpM.p[1][0] = matA.p[1][0] * matB.p[0][0] + matA.p[1][1] * matB.p[1][0] + matA.p[1][2] * matB.p[2][0] + matA.p[1][3] * matB.p[3][0];
tmpM.p[1][1] = matA.p[1][0] * matB.p[0][1] + matA.p[1][1] * matB.p[1][1] + matA.p[1][2] * matB.p[2][1] + matA.p[1][3] * matB.p[3][1];
tmpM.p[1][2] = matA.p[1][0] * matB.p[0][2] + matA.p[1][1] * matB.p[1][2] + matA.p[1][2] * matB.p[2][2] + matA.p[1][3] * matB.p[3][2];
tmpM.p[1][3] = matA.p[1][0] * matB.p[0][3] + matA.p[1][1] * matB.p[1][3] + matA.p[1][2] * matB.p[2][3] + matA.p[1][3] * matB.p[3][3];
tmpM.p[2][0] = matA.p[2][0] * matB.p[0][0] + matA.p[2][1] * matB.p[1][0] + matA.p[2][2] * matB.p[2][0] + matA.p[2][3] * matB.p[3][0];
tmpM.p[2][1] = matA.p[2][0] * matB.p[0][1] + matA.p[2][1] * matB.p[1][1] + matA.p[2][2] * matB.p[2][1] + matA.p[2][3] * matB.p[3][1];
tmpM.p[2][2] = matA.p[2][0] * matB.p[0][2] + matA.p[2][1] * matB.p[1][2] + matA.p[2][2] * matB.p[2][2] + matA.p[2][3] * matB.p[3][2];
tmpM.p[2][3] = matA.p[2][0] * matB.p[0][3] + matA.p[2][1] * matB.p[1][3] + matA.p[2][2] * matB.p[2][3] + matA.p[2][3] * matB.p[3][3];
tmpM.p[3][0] = matA.p[3][0] * matB.p[0][0] + matA.p[3][1] * matB.p[1][0] + matA.p[3][2] * matB.p[2][0] + matA.p[3][3] * matB.p[3][0];
tmpM.p[3][1] = matA.p[3][0] * matB.p[0][1] + matA.p[3][1] * matB.p[1][1] + matA.p[3][2] * matB.p[2][1] + matA.p[3][3] * matB.p[3][1];
tmpM.p[3][2] = matA.p[3][0] * matB.p[0][2] + matA.p[3][1] * matB.p[1][2] + matA.p[3][2] * matB.p[2][2] + matA.p[3][3] * matB.p[3][2];
tmpM.p[3][3] = matA.p[3][0] * matB.p[0][3] + matA.p[3][1] * matB.p[1][3] + matA.p[3][2] * matB.p[2][3] + matA.p[3][3] * matB.p[3][3];
Is this a bad/slow idea?
Will it be more efficient to use a loop?
It will mostly depend on what the compiler manages to figure out from it.
If you can't time the operation in a context similar or identical to use (which remains the best way to figure it out), then I would guess a loop with a functor (functor object or lambda) is likely the best bet for the compiler to be able to figure out a cache friendly unroll and a cheap access to the operation.
A half decent modern compiler will also most likely vectorize it on a CPU.

whats best way to start solving by iteration?

I am working on a home project where I need to closely solve an equation by iteration.
M = E - e* sin(E) or another way (E - e*sin(E))/M = 1.
M is previously solved for and e is given in the data message.
So would you plug in a number for E and check to see how close the value ends up to 1, then continue to adjust the plug in value of E untill the expression is within a set value to 1.00000?
Is there an "ideal" method to solving something like this in software?
The rest of my calculation function is shown as
FP64 is defined as double
bool SV_pos_GAL_L1(int chan, FP64* x, FP64* y, FP64* z) //finds SV ECEF position in orbit at ref GAL system time
{
FP64 smaxis = pow(GALChannel[chan].L1galData.sqrrtA, 2); //semi major axis
FP64 nc = sqrt( MU/(pow(smaxis, 3)) ) + GALChannel[chan].L1galData.delta_n; //n corrected
FP64 Tk = GALChannel[chan].L1galData.TOW - GALChannel[chan].L1galData.Toe; //time since ephemeris
FP64 M = GALChannel[chan].L1galData.M0 + nc * Tk; //mean anomaly
FP64 E;
FP64 v = atan( ((sqrt(1-pow(GALChannel[chan].L1galData.e,2)) * sin(E)) / (1-(GALChannel[chan].L1galData.e*cos(E)))) / ((cos(E)-GALChannel[chan].L1galData.e) / (1-(cos(E)*GALChannel[chan].L1galData.e))) );//true anomaly
FP64 Omega = GALChannel[chan].L1galData.Omega0 + (Tk * (GALChannel[chan].L1galData.dot_Omega - GALChannel[chan].L1galData.w)) - ( GALChannel[chan].L1galData.w * GALChannel[chan].L1galData.Toe); //corrected longitude of ascinding node
FP64 ArgLat = v + Omega; //argument of latitude
FP64 Su = (GALChannel[chan].L1galData.Cus * sin(2*ArgLat)) + ( GALChannel[chan].L1galData.Cuc * cos(2*ArgLat)); //argument of latitude correction
FP64 Sr = (GALChannel[chan].L1galData.Crs * sin(2*ArgLat)) + ( GALChannel[chan].L1galData.Crc * cos(2*ArgLat)); //radius correction
FP64 Si = (GALChannel[chan].L1galData.Cis * sin(2*ArgLat)) + ( GALChannel[chan].L1galData.Cic * cos(2*ArgLat)); //inclination correction
FP64 u = ArgLat + Su; //corrected arg latitude
FP64 r = smaxis * (1 - (GALChannel[chan].L1galData.e * cos(E))) + Sr; //corrected radius
FP64 i = GALChannel[chan].L1galData.i0 + Si + (GALChannel[chan].L1galData.dot_i * Tk); //corrected inclination
FP64 x1 = r * cos(u);
FP64 y1 = r * sin(u);
x = (x1 * cos(Omega)) - (y1 * cos(i) * sin(Omega));
y = (x1 * sin(Omega)) - (y1 * cos(i) * cos(Omega));
z = y1 * sin(i);
return true;
}