# Yacas

Yacas is a computer algebra system. It is included in Euler via a dynamically linked library and can be accessed through the command line. Yacas is open source. For information about this project view their home page.

The original Yacas documentation is included here. This page will only describe some basic commands and the Euler interface to Yacas.

There is also an introductory Euler notebook for Yacas.

I like to credit Ron Larham for pointing out Yacas to me and doing the first version of the interface. Thanks!

## The yacas command

The basic command is yacas(), which takes a string, sends it to Yacas, and returns the Yacas output, or an error message.

    >yacas("D(x) x^4")
    4*x^3

You see, how Yacas evaluated the string, and computed the derivative. It returned an expression, which can be used in Euler with the evaluate function.

    >x=2.5; evaluate(yacas("D(x) x^4"))
    62.5

There is a shortcut for this. The advantage is that you do not have to assign a value to x globally.

    >yeval("D(x) x^4",x=2.5)
    62.5

The function yeval() evaluates the Yacas output. You can use assigned parameters to pass values to it. Here is another example.

    >yeval("Integrate(x,0,a) x^2",a=1)
    0.333333

You can also get the exact result by using one of the substitution mechanisms of Yacas.

    >yacas("Eliminate(a,1,Integrate(x,0,a) x^2)")
    1/3

If you decide now that you want to evaluate this, you can access the last Yacas output ("1/3") by the % letter.

    >yeval("%")
    0.333333

The Yacas session remembers its state. To start a new session, use

    >yacasclear;

You can also store variables in Yacas.

    >yacas("Set(A,{{1,1},{1,2}})")
    True
    >yacas("A*A")
    {{2,3},{3,5}}
    >yacas("A^20")
    {{63245986,102334155},{102334155,165580141}}

Here, we stored a matrix in Yacas form. To store an Euler matrix to Yacas, look into the transfer functions described in the next section.

Here is some more linear Algebra.

    >yacas("Inverse(A)")
    {{2,-1},{-1,1}}
    >yacas("%*A")
    {{1,0},{0,1}}
    >yacas("MatrixSolve(A,{3,2})")
    {4,-1}
    >yacas("CharacteristicEquation(A,x)")
    (1-x)*(2-x)-1
    >yacas("Solve(%,x)")
    {x==(Sqrt(5)+3)/2,x==(3-Sqrt(5))/2}
    >yeval("%[1][2]")
    2.61803

Look at the computation of the eigenvalue. The Yacas solver is not very sophisticated yet, but does the job, returning a list of two solutions in form of equations. To access the right hand side of the first solution, you need to index the expression in Yacas.

You can also store expressions as strings in Euler. Then you will need to create a correct string for new expressions using |. Here is a simple example.

    >expr="x^x";
    >"Solve((D(x) "|expr|")== 0,x)"
    Solve((D(x) x^x)== 0,x)
    >yacas("Solve((D(x) "|expr|")== 0,x)")
    {x==Exp(-1)}
    >yeval("D(x,2) "|expr,x=exp(-1))
    1.8816

The commands containing "x^x" are built using |.

## Converting from Euler to Yacas

You can convert real and complex Euler numbers, vectors and matrix to Yacas using converty().

    >A=[1,1;1,2]; inv(A),
    2 -1
    -1 1
    >yacas(converty(A)); yacas("Inverse(%)")
    {{2,-1},{-1,1}}

The output of the inverse matrix is in Yacas form, as you see. However, there is a function to convert it back to Euler.

    >y2matrix(yacas("%"))
    2 -1
    -1 1

Of course yacas("%") is used here to access the string "{{2,-1},{-1,1}}" in a fast way. There is also y2vector() for vectors.

converty() is also used to set a variable in Yacas with the function yset(). Look at this example.

    >yset("A",[1,2;2,1])
    True
    >yacas("EigenValues(A)")
    {-1,3}
    >yacas("EigenVectors(A,%)")
    {{-k2,k2},{k2,k2}}
    >yacas("Eliminate(k2,1,%)")
    {{-1,1},{1,1}}

## Using Yacas in Functions

Since Yacas is called dynamically, you need to use evaluate() to incorporate Yacas expressions into programs. The following function calls the interval Newton method, which needs a function and a derivative, passing only the function as an expression. The derivative is computed by Yacas.

    >function yinewton (expr,start)
    $return inewton(expr,yacas("D(x) "|expr),start);
    $endfunction
    >yinewton("x^x-2",~1,2~)
    ~1.559610469462368,1.559610469462371~

Likewise, you can do all sort of advanced algorithms.

If the expression is not passed as a parameter, you can call Yacas at compile time with @"...". Here is an example.

    >function test(x)
    $return @"D(x) x^x";
    $endfunction
    >test(2)
     6.77259
    >type test
    function test (x)
    return x^x*(Ln(x)+1);
    endfunction

As you see by the function dump, the trick is that Yacas is only called once, not with every call to "test".