Convert powers of trigonometric functions to linear sum of multiple angles - sympy

I cannot figure out if sympy has any functions to convert powers of trigonometric functions to a linear combination of multiple angles. For example, sin(x)**4 can be written as (3/2 - 2*cos(2*x) + 1/2 * cos(4*x))/4.

I managed to get this working this way:
simplify(expand((sin(x)**4).rewrite(exp)))

Related

Find the right Google OR-Tools algorithm

I have to find the set of integers that minimize this objective function:
The costraints are:
every x must be a non-negative integer
T, A and B are double known numbers.
I have been looking at the OR-Tools C++ library in order to solve this problem, specifically at the CP-SAT solver.
Is it the right tool from such problems?
If yes, would it be feasible to convert all the double to int in the objective function?
If not, what else do you suggest? (I'm also open to other open source C++ libraries)
It will fit in the CP-SAT solver. You will need to scale floating point coefficients to integers.
The objective function accepts floating point coefficients.
But (x1 + A1)^2 will propagate better if you keep it that way instead of A1^2 + 2 * A1 * x1 + x1^2. which fits into the linear with double coefficient limitation of CP-SAT, provided you use temporary variables sx1 = x1 * x1.
Then make sure to use at least 8 workers for that. (parameters num_search_workers:8).
Now, I believe there are least square solvers that are more suited for this.

Sympy -- define a proper domain

I want to perform some calculations with polynomials in Sympy, such as multiplication and so on. The coefficients of these polynomials are noncommutative . It may be matrices for example. How can I set the appropriate domain in the "Sympy.Poly" class for this problem.

How to numerical evaluate trigonometric functions as a multiple of pi using the sympy package?

Is there a way to evaluate trigonometric functions as a multiple of pi using sympy? You can use the N function to get a numerical evaluation, but I'm searching for a function which will give me values like a * pi (a = constant).
SymPy already does that for inverse trigonometric functions. But to get a symbolic (as opposed to floating point) result, you need to provide symbolic (as opposed to floating point) input. Compare
>>> asin(1/2)
0.523598775598299
>>> asin(Rational(1, 2))
pi/6
Here 1/2 produces a Python float 0.5, and the result is another Python float. But Rational(1, 2) constructs a SymPy object representing a rational number, and then evaluation is done symbolically.
More in Python numbers vs. SymPy Numbers

Gamma function in shaders

Using spherical harmonics for lighting I faced a problem for a big enough bandwidths. The correctness of an approximation by first n^2 terms became worse and worse starting from n=7. I look into associated Legendre polynomials definition and found out, that there is a ratio of factorials (l - m)! / (l + m)! in normalization constant. For n = 7 (l + m)! can be up to 12!. I have to use float (IEEE-754 32-bit floating-point type), due to GPUs nature.
Now I think, that tgamma from C/C++ might be more appropriate, then naive calculation of factorial by definition. Even more: maybe there is a good (approximation) formula for ratio of gamma functions (of two big arguments).
Is there a good stable approach to calculate gamma function (for big positive integers) in shaders?
Surely I just can save a lookup table (matrix) for all the possible combinations of values in numerator and denominator, but I want to have alternative (space-efficient) approach.

Arbitrary precision gamma function

I'm implementing an arbitrary precision arithmetic library in C++ and I'm pretty much stuck when implementing the gamma function.
By using the equivalences gamma(n) = gamma(n - 1) * n and gamma(n) = gamma(n + 1) / n, respectively, I can obtain a rational number r in the range (1; 2] for all real values x.
However, I don't know how to evaluate gamma(r). For the Lanczos approximation (https://en.wikipedia.org/wiki/Lanczos_approximation), I need precomputed values p which happen to calculate a factorial of a non-integer value (?!) and can't be calculated dynamically with my current knowledge... Precomputing values for p wouldn't make much sense when implementing an arbitrary precision library.
Are there any algorithms that compute gamma(r) in a reasonable amount of time with arbitrary precision? Thanks for your help.
Spouge's approximation is similar to Lanczos's approximation, but probably easier to use for arbitrary precision, as you can set the desired error.
Lanczos approximation doesn't seem too bad. What exactly do you suspect?
Parts of code which calculate p, C (Chebyshev polynomials) and (a + 1/2)! can be implemented as stateful objects so that, for example, you can calculate p(i) from p(i-1) and Chebyshev coefficients and be computed once, maintaining their matrix.