redSolve: function to solve a system of equations using the REDUCE...

View source: R/redSolve.R

redSolveR Documentation

function to solve a system of equations using the REDUCE solve operator

Description

redSolve generates code for the REDUCE solve operator, executes it and converts the result to appropriate R objects if possible.

Usage

redSolve(id, eqns, unknowns, switches)

Arguments

id

the session identifier returned by redStart. Required with no default

eqns

the system of equations to be solved as character vector with one element per equation. If equations are in the normal form the =0 may be omitted. Required. No default.

unknowns

the unknowns for the system of equations specified by eqns as a character vector with one element for each unknown. Required. No default.

switches

a character vector of switches to be applied prior to executing solve. See Details below. Optional.

Details

solve is a REDUCE operator for solving one or more simultaneous algebraic equations in one or more variables, both linear and non-linear. Equations may contain arbitrary constants which is why it is necessary to specify the unknowns. For example, given a quadratic equation with constants a, b and c:

16: solve(a*x^2 + b*x + c, x);
  
                     2
    sqrt( - 4*a*c + b ) - b
{x=-------------------------,
              2*a

                         2
     - (sqrt( - 4*a*c + b ) + b)
 x=------------------------------}
                2*a

The standard solution is returned as a REDUCE list. We can change the order of the expression to get the usual form by using the korder declaration before calling solve:

17: korder b, a;
18: solve(a*x^2 + b*x + c, x);

          2
    sqrt(b  - 4*a*c) - b
{x=----------------------,
            2*a

              2
     - (sqrt(b  - 4*a*c) + b)
 x=---------------------------}
               2*a

If the solve operator could not find an explicit solution, it may still be able to find one in terms of an equation for the unknown. Such a solution is identified by specifying the subsidiary equation as the argument of the operator root_of. If there are several unknowns, each solution will be a list of equations for the unknowns. For example,

  solve(x^7 - x^6 + x^2 = 1, x) ;
  {x=root_of(x_**6  + x_ + 1,x_,tag_1),x=1}

Note that redSolve turns off the NAT switch as otherwise the results will not be parsed correctly. redSolve will ensure that NAT is off for the duration of its execution. Turning on the NAT switch using the switches parameter results in an error.

There are several switches related to solve, in particular cramer, multiplicities, fullroots, trigform and varopt. See the REDUCE manual section 7.17 for details of these and of the methods used for solving equations.

Value

redSolve returns an object of class "redcas.solve" which contains the following elements:

solutions

a list of the solutions as returned by REDUCE. Each solution is a character vector.

rsolutions

a list of the solutions, where possible converted to appropriate R objects. Because each element of a solution may be of a different type (real, complex, string), each solution is a list rather than a vector.

nsolutions

numeric. The number of solutions.

rc

numeric. The return code. 0 is success, 1 failure

root_of

a complex vector identifying which solutions are presented in terms of the REDUCE root_of operator. A solution may expressed one or more unknowns in terms of the roots of another equation. In this case the other equation is enclosed in root_of(). This slot is a complex vector with the real part identifying the solution containing root_of() and the imaginary part the index of the unknown in that solution. This item is intended for use in a future release.

eqns

the vector of equations passed to redSolve

unknowns

the vector of unknowns passed to redSolve

switches

the vector of switches passed to redSolve

The redcas.solve object may be printed using the print method.

Author(s)

martin gregory

See Also

print to print a redcas.solve object, REDUCE manual section 7.17 for details of the REDUCE solve operator and redStart for creating a REDUCE session.

Examples

## Open a CSL session:
s1 <- redStart()

## can only run code if session was successfully started
if (is.numeric(s1)) {
   sol <- redSolve(s1, eqns=c("x+3y=7", "y-x=1"), unknowns=c("x", "y"))
   print(sol)

   ## close session:
   redClose(s1)
}

redcas documentation built on April 12, 2025, 1:40 a.m.