I have an expression involving fractional exponents that I want to make into a polynomial recognisable to sympy for solution. I could, if necessary, write the exponents using Rational but can't make that work.
What can I do?
>>> from sympy import *
>>> var('d x')
(d, x)
>>> (0.125567*(d + 0.04) - d**2.25*(2.51327*d + 6.72929)).subs(d,x**4)
0.125567*x**4 - (2.51327*x**4 + 6.72929)*(x**4)**2.25 + 0.00502268
SymPy does not combine exponents unless it knows it is safe to do so. For complex numbers it's only safe with integer exponents. Since we don't know if x is real or complex, the exponents are not combined.
Even for real x, (x**4)**(9/4) is not the same as x**9 (consider negative x). If x is declared real, using x = Symbol('x', real=True), then (x**4)**Rational(9, 4) correctly returns x**8*Abs(x) instead of x**9.
If x is declared positive, x = Symbol('x', positive=True), then (x**4)**Rational(9, 4) returns x**9.
It is not advisable to use floating point representation of rational numbers in SymPy, especially as exponents. This is why I replaced 2.25 by Rational(9, 4) above. With 2.25, the result is Abs(x)**9.0 when x is real, and x**9.0 if x is declared positive. The decimal dot indicates these are floating point numbers; so subsequent manipulations will have floating-point results instead of symbolic ones. For example (with x declared positive):
>>> solve((x**4)**Rational(9, 4) - 2)
[2**(1/9)]
>>> solve((x**4)**2.25 - 2)
[1.08005973889231]
Related
I am trying to implement Fast Inverse Square Root for a fixed point number, but I'm not getting anywhere.
I am trying to follow exactly the same principle as the article, except instead of writing the number in the floating point format x = (-1) ^ s * (1 + M) * 2 ^ (E-127), I am using the format x = M * 2 ^ -16, which is a 32-bit fixed point number with 16 decimal bits and 16 fractional bits.
The problem is that I cannot find the value of the "magic constant". According to my calculations, it doesn’t exist, but I’m not a mathematician and I think I’m doing everything wrong.
To solve Y = 1 / sqrt (x), I used the following reasoning (I don't know if it is correct).
In the original code we have that Y0 for approximation of newton is given by:
i = 0x5f3759df - (i >> 1);
Which means that we will have as a result a floating point number given by:
y0 = (1 + R2 - M / 2) * 2 ^ (R1 - E / 2);
This is because the operation >> divides exponent and mantissa by 2, and then we perform a subtraction of the numbers as integers.
Following the steps shown in the article, I set the format of x to:
x = M * 2 ^ -16
In an attempt to perform the same logic, I try to define Y0 for:
Y0 = (R2 - M / 2) * 2 ^ (R1 - (-16/2));
I'm trying to find a number, which can minimize the error given by:
error = (Y - Y0) / Y
Regardless of the value of R1, I can do shift operations to correct the exponent value of my final result, having the correct result at a fixed point.
Where am I wrong?
It can't be done.
The fast inverse sqrt is due to the floating point representation, that has already split the number into powers of two (exponent) and the significant.
It can be done.
With the same tricks as done for floating points, it's possible to convert your fixed point into 2^exp * x. Given uint32_t a, uint8_t exp = bias- builtin_count_leading_zeros(a); uint32_t b = a << exp, with the constants (and domain of a) so carefully chosen, that there will be no underflows or overflows.
Thus, you will actually have a custom floating point representation, which is tailored for this specific purpose, omitting the sign bit at least and having the best possible number of bits for the exponent, which might as well be 8.
Could someone please explain how the functions std::fmod and std::remainder work. In the case of the std::fmod, can someone explain the steps to show how:
std::fmod(+5.1, +3.0) = 2.1
Same thing goes for std::remainder which can produce negative results.
std::remainder(+5.1, +3.0) = -0.9
std::remainder(-5.1, +3.0) = 0.9
As the reference states for std::fmod:
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.
So to take the example in the question, when x = +5.1 and y = +3.0,
x/y (5.1/3.0 = 1.7) with its fractional part truncated is 1. So n is 1. So the fmod will yield x - 1*y which is 5.1 - 1 * 3.0 which is 5.1 - 3.0 which is 2.1.
And the reference states for std::remainder:
The IEEE floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where the value n is the integral value nearest the exact value x/y. When |n-x/y| = ½, the value n is chosen to be even.
So to take the example in the question, when x = +5.1 and y = +3.0
The nearest integral value to x/y (1.7) is 2. So n is 2. So the remainder will yield x - 2y which is 5.1 - 2 * 3.0 which is 5.1 - 6.0 which is -0.9.
But when x = -5.1 and y = +3.0
The nearest integral value to x/y (-1.7) is -2. So n is -2. So the remainder will yield x - 2y which is -5.1 - (-2) * 3.0 which is -5.1 + 6.0 which is +0.9
The reference also states that: In contrast to std::fmod(), the returned value is not guaranteed to have the same sign as x.
For those who may have a small difficulty understanding the good example by P.W., here is a slightly less mathematical approach.
fmod() function tells you how much remains after dividing your numerator evenly by your denominator.
remainder() function tells you how far your numerator is from the next closest number the denominator evenly divides into.
Examples:
fmod(10,3.5) = 3.
3.5 can fit twice into 10 (2*3.5 = 7), leaving a remainder of 3.
remainder(10,3.5) = -0.5.
3.5 cannot fit evenly into 10, but it can fit evenly into 7 (2*3.5) and 10.5 (3*3.5).
10.5 is closer to 10 than 7.
How far away is 10 from 10.5?
It is -0.5 away from 10.5.
My apologies if this has been asked before, but I cannot find it.
I was wondering if there is a way to calculate the point at which a single precision floating point number that is used as a counter will reach a 'maximum' (the point at which it is no longer able to add another value due to loss of precision).
For example, if I continuously add 0.1f to a float I will eventually reach a point where the value does not change:
const float INCREMENT = 0.1f;
float value = INCREMENT;
float prevVal = 0.0f;
do {
prevVal = value;
value += INCREMENT;
} while (value != prevVal);
cout << value << endl;
On GCC this outputs 2.09715e+06
Is there a way to compute this mathematically for different values of INCREMENT? I believe it should in theory be when the exponent portion of the float requires a shift beyond 23 bits, resulting in losing the mantissa and simply adding 0.
Given some positive y used as an increment, the smallest X for which adding y does not produce a result greater than X is the least power of 2 not less than y divided by half the “epsilon” of the floating-point format. It can be calculated by:
Float Y = y*2/std::numeric_limits<Float>::epsilon();
int e;
std::frexp(Y, &e);
Float X = std::ldexp(.5, e);
if (X < Y) X *= 2;
A proof follows. I assume IEEE-754 binary floating-point arithmetic using round-to-nearest-ties-to-even.
When two numbers are added in IEEE-754 floating-point arithmetic, the result is the exact mathematical result rounded to the nearest representable value in a selected direction.
A note about notation: Text in source code format represents floating-point values and operations. Other text is mathematical. So x+y is the exact mathematical sum of x and y, x is x in floating-point format, and x+y is the result of adding x and y in a floating-point operation. Also, I will use Float for the floating-point type in C++.
Given a floating-point number x, consider adding a positive value y using floating-point arithmetic, x+y. Under what conditions will the result exceed x?
Let x1 be the next value greater than x representable in the floating-point format, and let xm be the midpoint between x and x1. If the mathematical value of x+y is less than xm, then the floating-point calculation x+y rounds down, so it produces x. If x+y is greater than xm, either it rounds up and produces x1, or it produces some greater number because y is large enough to move the sum beyond x1. If x+y equals xm, the result is whichever of x or x1 has an even low digit. For reasons we will see, this is always x in the situations relevant to this question, so the calculation rounds down.
Therefore, x+y produces a result greater than x if and only if x+y exceeds xm, meaning that y exceeds half the distance from x to x1. Note that the distance from x to x1 is the value of 1 in the low digit of the significand of x.
In a binary floating-point format with p digits in its significand, the position value of the low digit is 21−p times the position value of the high digit. For example, if x is 2e, the highest bit in its significand represents 2e, and the lowest bit represents 2e+1−p.
The question asks, given a y, what is the least x for which x+y does not produce a result greater than x? It is the least x for which y does not exceed half the value of the low digit of the significand of x.
Let 2e be the position value of the high bit of the significand of x. Then y ≤ ½•2e+1−p = 2e−p, so y•2p ≤ 2e.
Therefore, given some positive y, the least x for which x+y does not produce a result greater than x has its leading bit, 2e, equal to or exceeding y•2p. And in fact it must be exactly 2e because all other floating-point numbers whose leading bit has position value 2e have other bits set in their significands, so they are greater. 2e is the least number for which the leading bit represents 2e.
Therefore, x is the least power of two that equals or exceeds y•2p.
In C++, std::numeric_limits<Float>::epsilon() (from the <limits> header) is the step from 1 to the next representable value, meaning it is 21−p. So y•2p equals y*2/std::numeric_limits<Float>::epsilon(). (This operation is exact unless it overflows to ∞.)
Let’s assign this to a variable:
Float Y = y*2/std::numeric_limits<Float>::epsilon();
We can find the position value represented by the highest bit of Y’s significand by using frexp (from the <cmath> header) to extract the exponent from the floating-point representation of Y and ldexp (also <cmath>) to apply that exponent to a new significand (.5 because of the scale that frexp and ldexp use):
int e;
std::frexp(Y, &e);
Float X = std::ldexp(.5, e);
Then X is a power of two, and it is less than or equal to Y. It is in fact the greatest power of two not greater than Y, since the next greater power of 2, 2X, is greater than Y. However, we want the least power of two not less than Y. We can find this with:
if (X < Y) X *= 2;
The resulting X is the number sought by the question.
Marek's Answer is pretty close, and a decent way to find it using a program (that is more efficient than the one I originally posted). However, I don't necessarily need the answer in a program form, just a mathematical one.
From what I can tell, the answer comes down to the exponent of the delta used, and the number of mantissa bits. We need to round to the nearest power of 2, which is kind of complicated. Basically if the mantissa is 0, we do nothing, otherwise we add 1 to the exponent. So, assuming we now have the delta as a power of 2, represented as 1.0 x 2exp, and a mantissa of N bits, the maximum value is 1.0 x 2(N + exp). Note that FLT_EPSILON in C is equal to 1.0 x 2-N. So we can also find this by dividing our nearest power of 2 by FLT_EPSILON.
For a delta of 0.1, the nearest power of 2 is 0.125, or 1.0 x 2-3. Therefore we want 1.0 x 2(23 + (-3)) or 1.0 x 221 which is equal to 2097152.
Yes it is possible.
there is std::numeric_limits::epsilon() which defines smallest value which can increase value 1.0.
Using this you can calculate this limit for any number.
In C there is DBL_EPSILON
So in your case this goes like this:
template<class T>
auto maximumWhenAdding(T delta) -> T
{
static_assert(std::is_floating_point_v<T>, "Works only for floating points.");
int power2= std::ilogb(delta);
float roudedDelta = ldexp(T { 1.0 }, power2);
if (roudedDelta != delta) {
roudedDelta *= 2;
}
return 2 * roudedDelta / std::numeric_limits<T>::epsilon();
}
live example C++
Note in live test examples delta fails to increase maxForDelta, but subtraction is successful, so this is exactly what you need.
I think there must be a simple answer to this, but I searched long and couldn't find it.
Example: A simple Laurent Polynomial, so that
>> p = 2*y*x**2+4*y/x
A factorization gives
>> factor(p)
2*y*(x**3 + 2)/x
How do I extract the factor 2*y/x ? Is there a simple way to get the common factor in the expression when the expression isn't a polynomial? I have tried a lot, but haven't found anything remotely satisfying. The factorization must exist somewhere in the steps of factor(), right?
As #unutbu points out, from this question you can see that factor_list gives the factors of a polynomial in a easier to use fashon.
Unfortunately, SymPy doesn't actually deal with Laurent polynomials (see https://github.com/sympy/sympy/issues/5131). You might actually not get what you are expecting for higher order terms. What SymPy does is it sees that p is a rational function, so it factors the numerator and denominator separately.
If you also want to do this, you can use something like
n, d = fraction(cancel(p))
factor_list(n)
factor_list(d)
and manipulate the factors separately.
The cancel will ensure that there are no duplicates, which would otherwise happen automatically with factor(p), and it also puts the expression in rational form so that fraction will work. You could also use p.as_numer_denom() to skip this step (if it ends up being slow).
Alternately, you may want to consider x and 1/x as separate generators of the polynomial. Here's a (corrected) function from the above issue
def aspoly1t(p, t, z=Symbol('z')):
"""
Rewrite p, a polynomial in t and 1/t, as a polynomial in t and z=1/t
"""
pa, pd = cancel(p).as_numer_denom()
pa, pd = Poly(pa, t), Poly(pd, t)
assert pd.is_monomial
d = pd.degree(t)
one_t_part = pa.slice(0, d + 1)
t_part = pa - one_t_part
t_part = t_part.to_field().quo(pd)
one_t_part = Poly.from_list(reversed(one_t_part.rep.rep), *one_t_part.gens, domain=one_t_part.domain)
one_t_part = one_t_part.replace(t, z) # z will be 1/t
ans = t_part.as_poly(t, z) + one_t_part.as_poly(t, z)
return ans
(some day Poly will support this natively as Poly(p, x, 1/x)) You can then use factor_list on this:
>>> aspoly1t(p, x)
Poly(2*y*x**2 + 4*y*z, x, z, domain='ZZ[y]')
>>> factor_list(aspoly1t(p, x))
(2, [(Poly(y, x, y, z, domain='ZZ'), 1), (Poly(x**2 + 2*z, x, y, z, domain='ZZ'), 1)])
Note that the factors here are not the same, so it really does matter how you want to interpret and factor your Laurent polynomials.
Using doubles I would expect to have about 15 decimal points of precision. I know that many decimal numbers are not exactly representable in floating point notation, so I would get an approximation for 1/3 for example. However, using a double I would expect an approximation that was correct to about 15 decimal points. I would also expect to retain that level of accuracy when doing arithmetic.
However, in the following example, I try to calculate the area of a triangle using Heron's formula and OpenMesh::Vec3d which are backed by OpenMesh::VectorDataT<double,3> and end up with a result that is only accurate to 5 decimal points.
The correct result is area = 8.19922e-8, but I'm getting area=8.1992238711962083e-8. Any ideas where this is coming from?
The suggestion that this might result from the instability in Heron's Formula is a good one, but unfortunately is not the case in this example. I have added code which calculates the stable variation on Heron for those who might be interested. In this example, u.norm()>v.norm()>w.norm().
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
int main()
{
//triangle vertices
OpenMesh::Vec3d x(0.051051, 0.057411, 0.001355);
OpenMesh::Vec3d y(0.050981, 0.057337, -0.000678);
OpenMesh::Vec3d z(0.050949, 0.057303, 0.0);
//edge vectors
OpenMesh::Vec3d u = x-y;
OpenMesh::Vec3d v = x-z;
OpenMesh::Vec3d w = y-z;
//Heron's Formula
double semiP = (u.norm() + v.norm() + w.norm())/2.0;
double area = sqrt(semiP * (semiP - u.norm()) * (semiP - v.norm()) * (semiP - w.norm()) );
//Heron's Formula for small angles
double areaSmall = sqrt((u.norm() + (v.norm()+w.norm()))*(w.norm()-(u.norm()-v.norm()))*(w.norm()+(u.norm()-v.norm()))*(u.norm()+(v.norm()-w.norm())))/4.0;
}
Heron's formula is numerically unstable. If you have a very "flat" triangle with small angles, the sum of the two small sides is almost the long side, so one of the terms gets very small. If, for example, a and b are the small sides,
(s - c)
will be very small, because
s = (a + b + c)/2
is nearly equal to c.
The wikipedia article about herons formula mentions a stable alternative:
Arrange the sides such that a > b > c and use
A = 1/4*sqrt((a + (b + c))*(c - (a - b))*(c + (a - b))*(a + (b - c)))
To 75 decimal places, the correct area of your triangle is
0.000000081992238711963087279421583920293974467992093148008322378721298327364.
If I replace the nine double constants you have with their decimal equivalents, I get
0.000000081992238711965902754749500279615357792172906541206211853522524016959
It would appear that you are not getting what you're expecting because you're expecting something unreasonable.
Any calculation involving subtraction will result in a loss of precision, if the values are at all close to each other. How many significant digits do you expect from this subtraction?
1.23456789012345
- 1.23456789000000
----------------
0.00000000012345
Both operands have 15 digits of precision, but the result only has 5.