in reply to Solve for 'x' in series
What I understand is that your coefficients are numerical but your $x (as you wrote it) is just a symbol in an equation and not a variable in a computer program.
So, provided I understood correctly, what you are looking for is a package capable of symbolic calculations.
There is one in perl already:
use Math::Calculus::NewtonRaphson; use strict; use warnings; my $exp = Math::Calculus::NewtonRaphson->new(); $exp->addVariable('x'); # our variable in the equations $exp->setExpression('x+3*x-x^2'); if( $exp->getError() ){ print "got error, ".$exp->getError()."\n"; exi +t(1) } my $result = $exp->newtonRaphson('x', 0); print "result: ".$result."\n";
Which gets fatal errors unfortunately.
Do you have any more options?
Yes you do. At least two.
1) Use perl to produce the sum equation in symbolic form (e.g. 1/(10*x)) and then use the symbolic solver in package R.
library(rSymPy) x <- Var("x") sympy("solve(3+3*x-x*x, x)")
The answer in this simple example is:
[1] "[3/2 - 21**(1/2)/2, 3/2 + 21**(1/2)/2]"
But you still need perl in order to produce the final equation as a string containing numbers and the letter 'x' (which is your symbolic variable you want to solve for). In the following code I have omitted the $c==0 or $c==1 logic.
use strict; use warnings; my $i; my @q = map { rand() } (1..1000); # your 1000 'q' coefficients as just + random numbers here my @c = map { rand() } (1..1000); # your 1000 'c' coefficients as just + random numbers here my ($a1, $a2, $a3); my $sum_equ = ""; for ($i=1; $i<1000; $i++) { $a1 = (1-$c[$i])*(1-2*$q[$i]); $a2 = -2*$q[$i]; $a3 = $c[$i]*(2*$q[$i] -1); # concatenate the equation of this time with the total equation: $sum_equ = $sum_equ . "($a1/($a2 * x + x + $q[$i]) + $a3/(2*q[$i +]*x-x+1)) + "; } $sum_equ =~ s/\+\s*$//; # remove last + print "library(rSymPy)\n\ x <- Var('x')\n\ sympy('solve($sum_equ, x)')\n ";
Save the above code to a file called 'equ.pl' and then from the command-line do:
perl equ.pl | R
It will probably give you the solution crash your computer (because the equation is huge).
2) Your second option (and your only option if your computer crashed in option #1) requires a bit more thinking because you need to handcraft your equations before concatenating in order to do some simplifications of the form 3*x + x => 4*x. How are you going to achieve that? You need to start from your basis equations and accumulate the coefficients of 'x' 'x^2' (if any) in a single variable etc..
|
|---|