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..


In reply to Re: Solve for 'x' in series by bliako
in thread Solve for 'x' in series by sbae

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.