# 1.3 The FriCAS Language¶

The FriCAS language is a rich language for performing interactive computations and for building components of the FriCAS library. Here we present only some basic aspects of the language that you need to know for the rest of this chapter. Our discussion here is intentionally informal, with details unveiled on an as needed basis. For more information on a particular construct, we suggest you consult the index.

## 1.3.1 Arithmetic Expressions¶

For arithmetic expressions, use the + and - operator as in mathematics. Use * for multiplication, and ^ for exponentiation. To create a fraction, use /. When an expression contains several operators, those of highest precedence are evaluated first. For arithmetic operators, ^ has highest precedence, * and / have the next highest precedence, and + and - have the lowest precedence.

FriCAS puts implicit parentheses around operations of higher precedence, and groups those of equal precedence from left to right.

1 + 2 - 3 / 4 * 3 ^ 2 - 1

$-194$

Type: Fraction Integer

The above expression is equivalent to this.

((1 + 2) - ((3 / 4) * (3 ^ 2))) - 1

$-194$

Type: Fraction Integer

If an expression contains subexpressions enclosed in parentheses, the parenthesized subexpressions are evaluated first (from left to right, from inside out).

1 + 2 - 3/ (4 * 3 ^ (2 - 1))

$114$

Type: Fraction Integer

## 1.3.2 Previous Results¶

Use the percent sign % to refer to the last result. result:previous Also, use %%" to refer to previous results. percentpercent@{%%} %%(-1) is equivalent to %, %%(-2) returns the next to the last result, and so on. %%(1) returns the result from step number 1, %%(2) returns the result from step number 2, and so on. %%(0) is not defined.

This is ten to the tenth power.

10 ^ 10

$10000000000$

Type: PositiveInteger

This is the last result minus one.

% - 1

$9999999999$

Type: PositiveInteger

This is the last result.

%%(-1)

$9999999999$

Type: PositiveInteger

This is the result from step number 1.

%%(1)

$10000000000$

Type: PositiveInteger

## 1.3.3 Some Types¶

Everything in FriCAS has a type. The type determines what operations you can perform on an object and how the object can be used. Chapter~ugTypes is dedicated to the interactive use of types. Several of the final chapters discuss how types are built and how they are organized in the FriCAS library.

Positive integers are given type PositiveInteger.

8

$8$

Type: PositiveInteger

Negative ones are given type Integer. This fine distinction is helpful to the FriCAS interpreter.

-8

$-8$

Type: Integer

Here a positive integer exponent gives a polynomial result.

x^8

$x^8$

Type: Polynomial Integer

Here a negative integer exponent produces a fraction.

x^(-8)

$\frac{1}{x^8}$

Type: Fraction Polynomial Integer

## 1.3.4 Symbols, Variables, Assignments, and Declarations¶

A symbol is a literal used for the input of things like the variables in polynomials and power series.

We use the three symbols x, y, and z in entering this polynomial.

(x - y*z)^2

${{{y} ^ {2}} \ {{z} ^ {2}}} -{2 \ x \ y \ z}+{{x} ^ {2}}$

Type: Polynomial Integer

A symbol has a name beginning with an uppercase or lowercase alphabetic symbol:naming character, %, or !. Successive characters (if any) can be any of the above, digits, or ?. Case is distinguished: the symbol points is different from the symbol Points.

A symbol can also be used in FriCAS as a variable. A variable refers to a value. To assign a value to a variable, variable:naming the operator := assignment is used. FriCAS actually has two forms of assignment: immediate assignment, as discussed here, and delayed assignment. See Section ugLangAssign for details. A variable initially has no restrictions on the kinds of declaration values to which it can refer.

This assignment gives the value 4 (an integer) to a variable named x.

x := 4

$4$

Type: PositiveInteger

This gives the value z+3/5 (a polynomial) to x.

x := z + 3/5

$z+{3 \over 5}$

Type: Polynomial Fraction Integer

To restrict the types of objects that can be assigned to a variable, use a declaration

y : Integer


Type: Void

After a variable is declared to be of some type, only values of that type can be assigned to that variable.

y := 89

$89$

Type: Integer

The declaration for y forces values assigned to y to be converted to integer values.

y := sin %pi

$0$

Type: Integer

If no such conversion is possible, FriCAS refuses to assign a value to y.

y := 2/3


Warning

Cannot convert right-hand side of assignment 2 - 3    to an object of the type Integer of the left-hand side.

A type declaration can also be given together with an assignment. The declaration can assist FriCAS in choosing the correct operations to apply.

f : Float := 2/3

$0.66666666666666666667$

Type: Float

Any number of expressions can be given on input line. Just separate them by semicolons. Only the result of evaluating the last expression is displayed.

These two expressions have the same effect as the previous single expression.

f : Float; f := 2/3

$0.66666666666666666667$

Type: Float

The type of a symbol is either Symbol or Variable(name) where name is the name of the symbol.

By default, the interpreter gives this symbol the type Variable(q).

q

$q$

Type: Variable q

When multiple symbols are involved, Symbol is used.

[q, r]

$[q,r]$

Type: List OrderedVariableList [q,r]

What happens when you try to use a symbol that is the name of a variable?

f

$0.66666666666666666667$

Type: Float

Use a single quote ‘ before quote the name to get the symbol.

'f

$f$

Type: Variable f

Quoting a name creates a symbol by preventing evaluation of the name as a variable. Experience will teach you when you are most likely going to need to use a quote. We try to point out the location of such trouble spots.

## 1.3.5 Conversion¶

Objects of one type can usually be converted to objects of several other types. To convert an object to a new type, use the :: infix operator. Conversion is discussed in detail in ugTypesConvert. For example, to display an object, it is necessary to convert the object to type OutputForm.

This produces a polynomial with rational number coefficients.

p := r^2 + 2/3

${{r} ^ {2}}+{2 \over 3}$

Type: Polynomial Fraction Integer

Create a quotient of polynomials with integer coefficients by using ::.

p :: Fraction Polynomial Integer

${{3 \ {{r} ^ {2}}}+2} \over 3$

Type: Fraction Polynomial Integer

Some conversions can be performed automatically when FriCAS tries to evaluate your input. Others conversions must be explicitly requested.

## 1.3.6 Calling Functions¶

As we saw earlier, when you want to add or subtract two values, you place the arithmetic operator + or - between the two arguments denoting the values. To use most other FriCAS operations, however, you use another syntax: function:calling write the name of the operation first, then an open parenthesis, then each of the arguments separated by commas, and, finally, a closing parenthesis. If the operation takes only one argument and the argument is a number or a symbol, you can omit the parentheses.

This calls the operation factor with the single integer argument 120.

factor(120)

${{2} ^ {3}} \ 3 \ 5$

Type: Factored Integer

This is a call to divide with the two integer arguments 125 and 7.

divide(125,7)

$\left[ {quotient={17}}, \: {remainder=6} \right]$

Type: Record(quotient: Integer, remainder: Integer)

This calls quatern with four floating-point arguments.

quatern(3.4,5.6,2.9,0.1)

${3.4}+{{5.6} \ i}+{{2.9} \ j}+{{0.1} \ k}$

Type: Quaternion Float

This is the same as factorial(10).

factorial 10

$3628800$

Type: PositiveInteger

An operation that returns a Boolean value (that is, true or false) frequently has a name suffixed with a question mark (?). For example, the even? operation returns true if its integer argument is an even number, false otherwise.

An operation that can be destructive on one or more arguments usually has a name ending in a exclamation point (!). This actually means that it is allowed to update its arguments but it is not required to do so. For example, the underlying representation of a collection type may not allow the very last element to be removed and so an empty object may be returned instead. Therefore it is important that you use the object returned by the operation and not rely on a physical change having occurred within the object. Usually destructive operations are provided for efficiency reasons.

## 1.3.7 Some Predefined Macros¶

FriCAS provides several macros for your convenience.See ugUserMacros for a discussion on how to write your own macros. Macros are names macro:predefined (or forms) that expand to larger expressions for commonly used values.

 %i The square root of -1. %e The base of the natural logarithm. %pi π. %infinity ∞. %plusInfinity +∞. %minusInfinity -∞.

To display all the macros (along with anything you have defined in the workspace), issue the system command )display all.

## 1.3.8 Long Lines¶

When you enter FriCAS expressions from your keyboard, there will be times when they are too long to fit on one line. FriCAS does not care how long your lines are, so you can let them continue from the right margin to the left side of the next line.

Alternatively, you may want to enter several shorter lines and have FriCAS glue them together. To get this glue, put an underscore (_) at the end of each line you wish to continue.

2_
+_
3


is the same as if you had entered

2+3


FriCAS statements in an input file (see Section ugInOutIn ), can use indentation to indicate the program structure . (see Section ugLangBlocks ).

2 + 3 -- this is rather simple, no?

$5$
There is no way to write long multi-line comments other than starting each line with -- or ++.