How can I round numbers with unknown constants on Python - python-3.x

I'm having problems with a project.
I want any number that multiplies with G (or G**yyy)with less precision than 0,5e-14 to be equal to 0.
I don't know how I can round a number next to a unknown constant.
I tried:
- the function round(x1,14),didn't work, gave me "type Add doesn't define round method"
- the
Input: x0=0.025*G-4.03
x1=x0-(((func.subs(x,x0))**2)/(func.subs(x,x0+func.subs(x,x0))-func.subs(x,x0)))
print(simplify(x1)-x0)
Output: -0.025*G + 4.03 + 1.0*(3.94430452610506e-31*G**10 + 1.81753552562921e-27*G**9 - 7.10858338912758e-25*G**8 - 1.52201232709757e-22*G**7 + 4.2351647362715e-22*G**6 - 2.02610280983229e-18*G**5 + 3.53883589099269e-16*G**4 - 2.39530617562878e-14*G**3 + 6.21724893790087e-13*G**2 + 1660.85701907689*G - 266416.790310032)/(5.16987882845642e-26*G**8 - 1.73707928636136e-23*G**7 - 3.3881317890172e-21*G**6 + 9.75781955236954e-19*G**5 - 5.55111512312578e-17*G**4 + 4.66293670342566e-15*G**3 - 1.4210854715202e-13*G**2 + 6.82121026329696e-12*G + 66434.2807630764
I want any number that multiplies with G (or G**yyy)with less precision than 0,5e-14 to be equal to 0.
For example
what I have: 3.94430452610506e-31*G**10
I want this to presented as: 0

Related

Order of Operation Mathematical

new to programming and came across this while doing a worksheet:
x = 1 / 2 + 3 // 3 + 4 ** 2
what is x?
I read that in regards to the exponent you have to read it right to left, and I did that and I keep getting 0 for some reason even though the answer was supposed to be 17.5. Any help on why/how I am supposed to get 17.5 and the order I was supposed to work it out from would be greatly appreciated. thanks.
Using Python, the result is 17.5
You can check the order of the mathematical operators in python (Python Operator Precedence) for more information
(1 / 2) + (3 // 3) + (4 ** 2) = 0.5 + 1 + 16
Ref:
https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
To determine the order of precedence for this expression, keep in mind these rules (non-exhaustive):
evaluation goes from left to right
Multiplication/division has higher precedence than addition/subtraction (e.g. 1 + 2 / 3 gets evaluated as 1 + (2 / 3))
Exponent/power has higher precedence than multiplication and division (e.g. 1 / 2**4 gets evaluated as 1 / (2**4))
These rules together show us that this expression:
x = 1 / 2 + 3 // 3 + 4 ** 2
Will be evaluated as:
x = (1 / 2) + (3 // 3) + (4 ** 2)
Therefore, x = 0.5 + 1 + 16 = 17.5.
The operator precedence is defined in the Python documentation:
https://docs.python.org/3/reference/expressions.html#operator-precedence
So, ** has the highest precedence, then / and // and then +.

Distance to a straight line in standard form

For a 3D straight line expressed in the standard form
a1*x + b1*y + c1*z + d1 = 0
a2*x + b2*y + c2*z + d2 = 0
and a given point x0,y0,z0
what is the distance from the point to the straight line?
Distance from point P0 to parametric line L(t) = Base + t * Dir is
Dist = Length(CrossProduct(Dir, P0 - Base)) / Length(Dir)
To find direction vector:
Dir = CrossProduct((a1,b1,c1), (a2,b2,c2))
To get some arbitrary base point, solve equation system with 2 equations and three unknowns (find arbitrary solution):
a1*x + b1*y + c1*z + d1 = 0
a2*x + b2*y + c2*z + d2 = 0
Check minors consisting of a and b, a and c, b and c coefficients. When minor is non-zero, corresponding variable might be taken as free one. For example, if a1 * b2 - b1 * a2 <> 0, choose variable z as free - make it zero or another value and solve system for two unknowns x and y.
(I omitted extra cases of parallel or coinciding planes)

Simplify strategies: Convince sympy that complicated term is zero

What are good strategies/heuristics to convince sympy that a complicated term including trigonometric functions is 0?
For example, let's consider the following term:
-2*a**2*b*(a**2 + b**2 + c**2)**(-12.0)*(a**2*(a**2 + b**2 + c**2)**9.0 + b**2*(a**2 + b**2 + c**2)**9.0 + c**2*(a**2 + b**2 + c**2)**9.0 - (a**2 + b**2 + c**2)**10.0)*sin(0.5*sqrt(a**2 + b**2 + c**2))**2
Wolfram alpha agrees with me that it should be zero.
I tried sympy.simplify as well as sympy.trigsimp, but not sure what to try next.
I'm using python 3.6.3 and sympy 1.1.1.
No special effort is needed; just avoid floating point numbers, representing them by integers or rationals. Floating point numbers are an obstacle to symbolic math because floating point arithmetics doesn't work like regular arithmetics. (The Python 2 habit of making everything a float so that division works right really needs to be unlearned to use SymPy effectively.)
e = -2*a**2*b*(a**2 + b**2 + c**2)**(-12)*(a**2*(a**2 + b**2 + c**2)**9 + b**2*(a**2 + b**2 + c**2)**9 + c**2*(a**2 + b**2 + c**2)**9 - (a**2 + b**2 + c**2)**10)*sin(sqrt(a**2 + b**2 + c**2)/2)**2
e.simplify()
returns 0.
Besides changing 12.0 to 12, etc I also got changed 0.5*sqrt(a**2 + b**2 + c**2) to sqrt(a**2 + b**2 + c**2)/2. Other options here include replacing 0.5 by
Rational(1, 2), or
Rational('0.5'), or
S.Half (SymPy's built-in 1/2 object)
S(1)/2 (S turns 1 into SymPy integer, which makes the division by 2 go by SymPy rules instead of Python rules).

Haskell big number calculations

I am trying to do some calculations with big numbers
λ: let r = 291381631919914084
λ: let t = 1165526527679656343
λ: sqrt(4 * r * r - 4 * r + 1 + 8 * t) - 2 * r + 1
1.0
the answer should be 8.0000...
Is there a package that I should be using for such calculations? or is there something I should be doing in prelude?
The correct answer is indeed very close to 8.0. You're running into numerical precision issues: the square root is being computed using IEEE 754 ("double precision") binary64 format, and its 53-bit precision isn't sufficient to give an accurate result here.
In more detail: the true value of sqrt(4 * r * r - 4 * r + 1 + 8 * t) is, to 50 significant figures:
582763263839828175.00000000000000000686385063746811
The closest representable IEEE 754 binary64 value to that quantity is:
582763263839828224.0
... which is off by about 49.0 from the true value. Similarly, the value 2*r loses precision when converted to floating-point.
You might be tempted to fix this by increasing the precision, but as so often happens in numerical work, in this case it's better to rework the algorithm to avoid (or at least ameliorate) the numerical issues. The value you're computing is of the form sqrt(a * a + b) - a (with a = 2 * r - 1 and b = 8 * t). That quantity can be rewritten in the form b / (sqrt(a * a + b) + a), and (assuming that both a and b are positive), the latter expression will give a more accurate result.
Here's a quick demonstration that the two expressions give the same result.
Prelude> let a = 43
Prelude> let b = 7
Prelude> sqrt(a * a + b) - a
8.131845707602992e-2
Prelude> b / (sqrt(a * a + b) + a)
8.131845707603225e-2
We're using smaller values of a and b, so the numerical issues aren't so bad, but note that there's still a discrepancy in the last 4 digits. (The exact value here is 0.081318457076032250005683932322636450, to 35 significant figures.)
And using this form of the expression with your values:
Prelude> let r = 291381631919914084
Prelude> let t = 1165526527679656343
Prelude> let a = 2*r - 1; b = 8*t in b / (sqrt(a*a+b) + a)
8.0
As other answerers have pointed out, the answer isn't exactly 8.0, but 8.0 is the closest IEEE 754 binary64 floating-point value to the true answer.
I believe 8 is not a correct answer, either; the number you have given is not square:
Math.NumberTheory.Powers.Squares> r = 291381631919914084
Math.NumberTheory.Powers.Squares> t = 1165526527679656343
Math.NumberTheory.Powers.Squares> isSquare (4*r*r - 4*r + 1 + 8*t)
False
However, you can get this answer if it's the one you want:
Math.NumberTheory.Powers.Squares> integerSquareRoot (4*r*r - 4*r + 1 + 8*t) - 2*r + 1
8
The arithmoi package provides these functions.
Or, you can get as many digits of the exact answer as you like:
Data.Number.CReal> sqrt (4*r*r - 4*r + 1 + 8*t) - 2*r + 1 :: CReal
8.0000000000000000068638506374681082902485
The numbers package provides this type.
I put these numbers and formula in Rstudio and also got 1. Are you sure the answer is 8? Maybe add more parentheses to make sure you got your order of operation correct.

Arithmetic Mean with exponent of small numbers

Due to rounding error, cannot get mean of three numbers:
a=-1.11e4
b=-1.12e4
c=-1.13e4
Mean=1/3 *[exp(a)+exp(b)+exp(c)]
How to get the results in a log value?
You're trying to find log((exp(a) + exp(b) + exp(c)) / 3), but a, b, and c are so low that the result of exp underflows to 0. You can fix this by adjusting the values so exp doesn't underflow.
Let d = max(a, b, c). Then we have the following equality:
M = log((exp(a) + exp(b) + exp(c)) / 3)
= log(exp(d) * (exp(a-d) + exp(b-d) + exp(c-d)) / 3)
= log(exp(d)) + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3)
= d + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3)
So we can calculate the result as d + log((exp(a-d) + exp(b-d) + exp(c-d)) / 3). Since d is equal to one of a, b, or c, one of the exp arguments is 0, and the rest are at most 0. Thus, one of the exp outputs is 1, and the rest are at most 1. We don't have to worry about overflow or underflow; while an underflow might still occur in one or more exp calls, it won't be a problem any more, since the log argument won't be 0.

Resources