solve mathematical equation with 1 unknown (equations are dynamically built) - equation

I have to built dynamically equations like following:
x + x/3 + (x/3)/4 + (x/3/4)/2 = 50
Now I would like to evaluate this equation and get x. The equation is built dynamically. x is the leaf node in a taxonomy, the other 3 nodes are the super concepts. The divisor represents the number of children of the child nodes.
Is there a library that allows to build such equations dynamically and resolve x?
Thanks, Chris

Are your equations always of this form (linear in x)?
If so, when building the equation, just set x to 1 and evaluate the lhs.
This will give you lhs = 1 + 1/3 + (1/3)/4 + (1/3/4)/2 = 1.4583..
Then calculate x = rhs / lhs = 50 / 1.4583

It might help you to do some algebra on it.
Note that:
x= 3*x/3 = (x*4*3*2)/(4*3*2)
x+x/3 = 3x/3 + x/3 = 4x/3
and in your particular case:
x + x/3 + (x/3)/4 + (x/3/4)/2 = (x*4*3*2)/(4*3*2) + (x*4*2)/(4*3*2) + (x*2)/(4*3*2) + (x)/(4*3*2)
= (4*3*2x + 4*2x + 2*x + x)/(4*3*2)
Perhaps if you can find a way to have the left hand side rewritten as a single big fraction like this, the solution will come much easier.
Also, factor out the x
(4*3*2x + 4*2x + 2*x + x)/(4*3*2) = x*(4*3*2 + 4*2 + 2 + 1)/(4*3*2)
Then solve for x
50= x*(a/b)
50*(b/a) = x
Since you have some code generating the polynomial, you should be able to generate this big (a/b) fraction thing pretty easily too. I purposely did not simplify the multiplications so that it is clear where each component comes from.

If you're planning to use Java, you can try JAS. It claims to be able to solve polynomials equations.
FTA:
The Java Algebra System
(JAS) is an object oriented, type safe
and multi-threaded approach to
computer algebra. JAS provides a well
designed software library using
generic types for algebraic
computations implemented in the Java
programming language. The library can
be used as any other Java software
package or it can be used
interactively or interpreted through
an jython (Java Python) front end. The
focus of JAS is at the moment on
commutative and solvable polynomials,
Groebner bases and applications. By
the use of Java as implementation
language JAS is 64-bit and multi-core
cpu ready.

Related

Is it possible to write (display) exponential equations in scilab?

I've been trying to display in my console an exponential equation like the following one:
y(t) = a*e^t + b*e^t + c*e^t
I would write it as a string, however the coefficients a,b and c, are numbers in a vector V = [a b c]. So I was trying to concatenate the numbers with strings "e^t", but I failed to do it. I know scilab displays polynomial equations, but I don't know it is possible to display exponential one. Anyone can help?
Usually this kind of thing is done with mprintf command, which places given numerical arguments into a string with formatting instructions.
V = [3 5 -7]
mprintf("y(t) = %f*e^t + %f*e^t + %f*e^t", V)
The output is
y(t) = 3.000000*e^t + 5.000000*e^t + -7.000000*e^t
which isn't ideal, and can be improved in some ways by tweaking the formatters, but is readable regardless.
Notice we don't have to list every entry V(1), V(2), ... individually; the vector V gets "unpacked" automatically.
If you wanted to have 2D output like what we get for polynomials,
then no, this kind of thing is what Scilab does for polynomials and rational functions only, not for general expressions.
There is also prettyprint but its output is LaTeX syntax, like $1+s+s^{2}-s^{123}$. It works for a few things: polynomials, rational functions, matrices... but again, Scilab is not meant for symbolic manipulations, and does not really support symbolic expressions.

Why use a special function to generate pseudo-random numbers in Pollard rho factorization?

I've been getting myself acquainted with the Pollard Rho factorization from this page.
I think I understand almost everything there, but one thing I'm confused about – the fact that it uses f(x) = x^2 + a mod N to generate pseudo-random numbers for checking.
My question is, why can't we simply have a random number generator give us some two random numbers (xi, xj) each time, where xi, xj < N?
Why use this function f(x)?
The particular random number generator doesn't matter. Pollard in his original paper describing the algorithm says "Other polynomials of degree ≥ 2 and other starting values can be used." Brent says "The choice of a (pseudo-) random u ∈ [0,1) is not essential; it merely makes the average-case analysis tractable. Pollard and Brent describe the use of a function other than x2 + c to factor 228 + 1. The advantage of the x2 + c method is that it is simple to implement and gives a family of polynomials, making it easy to switch to another one if the first doesn't work.

How to Solve non-specific non-linear equations?

I am attempting to fit a circle to some data. This requires numerically solving a set of three non-linear simultaneous equations (see the Full Least Squares Method of this document).
To me it seems that the NEWTON function provided by IDL is fit for solving this problem. NEWTON requires the name of a function that will compute the values of the equation system for particular values of the independent variables:
FUNCTION newtfunction,X
RETURN, [Some function of X, Some other function of X]
END
While this works fine, it requires that all parameters of the equation system (in this case the set of data points) is hard coded in the newtfunction. This is fine if there is only one data set to solve for, however I have many thousands of data sets, and defining a new function for each by hand is not an option.
Is there a way around this? Is it possible to define functions programmatically in IDL, or even just pass in the data set in some other manner?
I am not an expert on this matter, but if I were to solve this problem I would do the following. Instead of solving a system of 3 non-linear equations to find the three unknowns (i.e. xc, yc and r), I would use an optimization routine to converge to a solution by starting with an initial guess. For this steepest descent, conjugate gradient, or any other multivariate optimization method can be used.
I just quickly derived the least square equation for your problem as (please check before use):
F = (sum_{i=1}^{N} (xc^2 - 2 xi xc + xi^2 + yc^2 - 2 yi yc + yi^2 - r^2)^2)
Calculating the gradient for this function is fairly easy, since it is just a summation, and therefore writing a steepest descent code would be trivial, to calculate xc, yc and r.
I hope it helps.
It's usual to use a COMMON block in these types of functions to pass in other parameters, cached values, etc. that are not part of the calling signature of the numeric routine.

Math function to filter negative numbers

Is there any standard math function for this operation:
f(x)=max(x,0)
I was wondering maybe there is a well-known function for this operation in mathematics literature.
Any idea?
This is usually denoted as (x)+, sometimes also x⊔0 or x∨0, where the symbol alludes to the shape of the kinks in the maximum of two functions, for instance in |x|=max(x,-x).
In Lebesgue integration theory, for example, a function is first split into its positive and negative part, so that the integration theory can be reduced to non-negative functions.
Another application is splines, the cubic B-spline has the representation
B3(x)=1/6 * ( (x+2)+3 - 4 * (x+1)+3 + 6 * (x)+3 - 4 * (x-1)+3 + (x-2)+3 )
I guess, you are looking for:
(abs(x)+x)/2
https://www.wolframalpha.com/input/?i=%28%7Cx%7C%2Bx%29%2F2
Another way it might be characterised is as
x H(x)
where H(x) is the Heaviside unit step function.
H(x) = ( x >= 0 ? 1 : 0 )
i.e. 1 for positive x, 0 for negative x and either 0, 1, or 1/2 at x=0. This is used in control theory, signal processing and Fourier analysis. Its quite common to use f(x) H(x) for functions which start at a particular time, say switching some electronics on. So in this area of study x H(x) might be the best way to answer your question.

Solving equation - Overflow error

Basically I just want to solve k. Note that the equation equals to 1.12
import math
from sympy import *
a = 1.45
b = 4.1
c = 14.0
al = math.log(a, 2)
bl = math.log(b, 2)
cl = math.log(c, 2)
k = symbols('k')
print solve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), k)
This raises OverflowError: Python int too large to convert to C long
Solution using other libraries welcomed too.
Since you are using numerical values, I am assuming that you are looking for a numerical solution. In that case, you should not use solve, because it tries to find a symbolic solution. The issue here is that it converts these floating point exponents into rational exponents, which have very large numerators and denominators, and it then at some point tries to make polynomials of degree corresponding to those large numbers, which is where it fails.
To solve numerically, you can use nsolve.
>>> print nsolve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), 2)
1.82427203413783
It's better to use numeric libraries like SciPy if you are interested in numeric solutions, though. You can use lambdify to convert your SymPy expressions into functions more suited for libraries like SciPy that use NumPy arrays.
It is a known issue.
You may try
solve(Eq(1/k**al + 1/k**bl + 1/k**cl, 1.12), k, rational=False)