Using A Better Calculator


The basics

Click in the console pane, you should see a flashing cursor after the "Enter Expression: " prompt. To compute an expression, simply type it in using your keyboard or the keypad pane. Hit enter to execute the calculation. The result of the calculation will appear in the console window underneath the expression you typed. ABC follows the usual order of operations, you can use brackets to override the default order.

Examples: 123+456. (3+4) *8.

You can often get away with omitting * for multiplication.

Examples: (2)(4). 3sin(2)

If you need to use the previous answer in your calculation you can use the symbol ans or @. They are both interpreted the same. You can ommit ans or @ at the start of a calculation.

Examples: 5^ans. @*4. +3 (adds 3 to the last answer).

"-" is interperted differently depending on the context. When it occurs between two numbers, e.g. "4-2", it is interpreted as subtraction. It is also interpreted in this way when it occurs as the first symbol in a calculation. For example, "-3" subtracts 3 from the previous answer. If you want to start an expression with a negative number you can get around this by starting your expression with a space. In all other cases "-" is interpreted as negation. For example, -5^2 will give 25 as expected, not -25 as with more primitive calculators.

Symbols

Symbols are an important concept in ABC. A symbol is a label that represents some object such as a number or a function. Some frequently used symbols are built into ABC, such as pi (the well known constant). All the currently defined symbols (excluding built ins) are listed in the symbol pane along with their values.

To define or change a symbol, type "<symbol> = <expression>" where <symbol> is the name of the symbol and <expression> is any valid expression.

Examples: a = 3. b = (a+6) *pi^2. a = a + 3 (this looks like a contradiction, but remember = here means define, not "equal to")

Complex numbers

One of the built in symbols in ABC is i. i is the imaginary unit. You can use it to enter expressions involving complex numbers.

Examples: (1+i)(3+2i) . Sqr(-1)

Functions

A function is an object which maps 1 or more parameters to a single return value. Functions are written in curly braces {}. The definition in the braces is divided into 2 sections by the bar |. The first section is a list of parameters and their types. The second is an expression in terms of the parameters.

Examples: {real x|x^2}, {real x, real y|xy}

When defining a function you will usually want to store it in a symbol.

For example: f = {real x|x^2}

To call a function, add a comma seperated list of parameters in parenthesis to the function.

Examples: f(3). {real x, real y|xy}(1, 2)

Vectors

ABC can handle vectors of real or complex numbers. The type of the vector is determined automatically. Vectors are defined with square brackets []. The components are seperated by commas.

Examples: [1, 2, 3] . [i, 2i, 1+i]

Vectors can be added and subtracted with the usual arithmetic operators. You can also apply the dot product ".". As with * in real number expressions, you can often drop the ".".

You can pass vectors into functions as parameters and return them.

Examples: r = {real t|[cos(t), sin(t)]}. f = {vector v|v.v}.

When dealing with vectors inside functions, 3 handy operators are defined. You can extract a component of a vector by placing the component in parenthesis after the vector name.

Example: f = {vector v|v(1)} (returns the first component of v)

You can use the mkv operator to generate a vector. This actually has 2 forms. The simple form is mkv(<expression>, <count>). This generates a vector of <count> components where each component is the value of <expression>. The advanced form is mkv(<function>, <count>). This time <function> is a function used to generate the vector. It takes a single integer parameter which represents the component it is generating.

Examples: mkv(1,3) (generates [1, 1, 1]). mkv({int k|k}, 3) (generates [1, 2, 3])

The last special operator is dim. This simply gives you the size of the vector

Example: dim [1,2,3] (gives 3). dim([1, 2, 3]) (same thing, the parenthesis are optional here)

More Functions

ABC functions are very powerfull. We will now look at some of their more advanced features. First, piecewise functions. Piecewise functions in ABC are defined in a similar manner to simple functions. Instead of one expression in the expressions section of the definition, you have several seperated by semi-colons ";". To define the conditions for an expression, append ":" <conditions> to the expression. <conditions> is a set of conditions connected with the and (&&) and or "||" operators. A condition is an <expression><relop><expression>. Where <expression> is any valid expression and <relop> is =, >< (not equal), >, <, <=, >=. Note that you cannot take shortcuts such as a=b=c or a<=b<= c. All but the last expression must have conditions. If the last expression has no condition, it will be executed only when the conditions for the other expressions are not fullfilled. Conditions are checked left to right, so if there is some overlap the first expression wins.

Examples: abs = {real x|x:x>0;-x} (an absolute value function)

Another advanced feature is recursion. Recursion can be used to implement repetative calculations such as summing up a sequence. It can also be used to define certain functions. Recursion involves a function calling itself. It is important to use conditions to limit the recursion, otherwise ABC will keep on executing untill it runs out of stack space. Also, if you have very deep recursion the stack might overflow. Since all functions are essentially annonymous, a special operator has to be defined for recursion. This is the self operator. You can call self just like any function. When you do your own function gets called.

Examples: fact = {int n|n*self(n-1):n>0;1} (a simple recursive definition of the factorial function).

Recursion is slow because ABC has to allocate space on the stack every the function recurses. To get around this you can utilise the tail recursion optimisation. By slightly changing the form of your definition, ABC will be able to replace the recursion with a flat loop which executes faster and never runs out of stack space. Be warned though, you can put ABC into an infinite loop if you don't define the conditions properly.

For example: fact = {int n|{int _n, int result|self(_n-1, _n*result):_n>0;result}(n, 1)}

Notice that this time the recursive part of the function contains only a function call. This is acheived via an inner function which passes the result of the calculation in a special parameter and eventually returns it. It takes a while to get your head around this, I sugest you look up tail recursion on wikipedia for a complete description of how it works.

Advanced Functions

Functions in ABC are first class objects. You can pass them into other functions as parameters and return them from functions.

Example: differentiate = {function f, real h|{real x|(f(x+h)-f(x))/h}} (differentiates a function of 1 variable)