Calca Reference
Calca Reference
* [Therefore][]
* [Numbers][]
* [Definitions][]
* [Arithmetic][]
* [Functions][]
* [Input][]
* [Units][]
* [Google Search][]
* [Matrices and Vectors][]
* [Ranges][]
* [Map & Reduce][]
* [Logic Programming][]
* [Computer Numbers][]
* [Complex Numbers][]
* [Derivatives][]
* [Standard Library][]
* [Glossary][]
## Therefore
The therefore operator simplifies, as much as it can, the expression to its left.
If it successfully solves for the variable, then it gives the simplified solution.
a*x^2 + b*x + c = 0
If it cannot solve for the variable, then it gives back the left and right hand
sides of the equation that it got closest to solving:
Here we can see a limitation of Calca, it can't solve cubic equations (yet).
## Numbers
See the section on [Cultures][] if you are not using English style numbers.
### Infinity
You can input an infinite number using name `Infinity` or the symbol `∞`.
5 / 0 => Infinity
Infinity * 10 => Infinity
∞ * 10 => Infinity
∞ / ∞ => 1
Numbers are printed using Calca's current number precision which defaults to `4`
decimal places.
You can control the output precision using the `precision` directive:
@precision = 8
pi => 3.14159265
@precision = 1
pi => 3.1
@p = 1000
pi => 3.14159265358979
@prec = 4
pi => 3.1416
You can control grouping using the @g, @group, or @grouping directives with a value
of `true` or `false`:
@group = false
1234567890 => 1234567890
The character used for grouping is chosen based on your [current culture]
[Cultures].
## Definitions
Definitions assign a value to a name. They are highlighted in bold to make this
clear:
name = 42
long name = -100
Anytime you use that name from then on, the assigned to value will be substituted:
### Redefine
name => 42
name = 389457
If you want to easily sum a set of definitions, then use the `+=` operator:
Summing definitions sum all definitions below it until it reaches a block of math
that isn't a definition or is not indented.
## Arithmetic
Addition and subtraction use the common operators `+` and `-`:
2 + 3 => 5
2 - 3 => -1
### Multiplication
2 * 3 => 6
2 × 3 => 6
2(3 + x) => 2 x + 6
2 * (3 + x) => 2 x + 6
It is also implied when a number is next to a variable:
2 x => 2 x
2 * x => 2 x
### Division
2 / 3 => 0.6667
2 / 0 => Infinity
2 ÷ 3 => 0.6667
### Modulo
2 mod 3 => 2
12 mod 3 => 0
### Exponents
You can raise a number to an exponents using the `^` or `**` operators:
2^3 => 8
a^3 => a^3
2**3 => 8
a**-b => a^(-b)
## Functions
You call functions by naming them and passing them a comma separated list of
arguments wrapped in parenthesis:
# Function Definition
fun(x, y) = x^2 + 2y^2
# Function Call
fun(3, 5) => 59
You can also name arguments when you call the function:
blend = start +
t * (end - start)
Calca creates a default parameter list for a definition that does not specify one.
That list is an ordered list of the variables that occur in the definition. For
`blend` above, this list is `(start, t, end)` since that is the order in which the
variables occurred. Because it's hard to remember this rule, it is highly
recommended that you provide a parameter list when you have more than one variable.
You can specify the parameters manually in definition so that you don't have to
name them every time you want to calculate the function.
blend2(t) = start +
t * (end - start)
Functions can have local definitions to keep them organized using `let name = value
in` expressions.
rotate(point, angle) =
let x = point[0] in
let y = point[1] in
let ca = cos(angle) in
let sa = sin(angle) in
[x * ca - y * sa,
y * ca + x * sa]
## Input
Calca files are plain text files. When loaded, they are parsed into a set of lines
or blocks which are evaluated one at a time starting from the top.
### Multiple Lines make Blocks
If you have some math that takes multiple lines of text to write, then just make
sure to indent those extra lines more than the first line. This is similar to
bibliography formats.
You can put multiple statements on one line by separating them with a semicolon:
err(exp, res) = abs((exp - res)/exp); err(10,12) => 0.2; err(10,11) => 0.1
### Markdown
To force Calca to treat a line as a text annotation, make sure it is unindented and
ends with punctuation.
#### Headings
You can use headings with the standard Markdown of prefixing lines with the `#`
symbol.
### Cultures
Other cultures, such as the French, prefer to use commas as the decimal separator.
You can switch Calca's culture any time using the @culture directive. Here is an
example of displaying and inputting numbers in different cultures.
alpha = 3,141.59
@fr-FR
@en-US
You can find your culture name in Microsoft's table of [language culture names]
[ccodes]. An incorrectly specified culture name makes Calca revert back to the
invariant culture.
[ccodes]: http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx
## Units
Calca *adores* units and makes it easy for you to specify numbers with units and to
convert between them.
You can even convert between units combined with multiplication and division. For
example, you might have want to calculate the speed of your run today:
Calca has a [database of over 200 units][cunits] covering simple units of length,
mass, volume, time, energy, etc. It even knows about advanced physical units such
as current and voltage:
12 V * 2 A in kW => 0.024 kW
12 V * 2 A in HP => 0.0322 HP
[cunits]: http://calca.io/units
Units are really easy to create. I think that there should be a new unit of mass:
### Currency
Calca knows about 35 various currencies that are all accessible using their [ISO
4217 code][iso4217].
You can convert between currencies using the unit conversion syntax:
For some currencies, Calca knows the correct symbol to use. For others, it will
display the ISO 4217 code:
[iso4217]: http://en.wikipedia.org/wiki/ISO_4217
### Percent
You can use SI prefixes in the unit conversion to specify your preferred order of
magnitude:
You can query Google from within Calca by writing a variable name and setting it
equal to `?`. Try uncommenting some of the following lines of code:
mass of earth = #?
goog = #?
10 km in miles = #?
This performs a Google search on your behalf and substitutes the `?` with the
result.
Matrix Multiplication:
Matrices are indexed using the `[row, column]` operator or the `[index]` operator.
All indexing begins at 0.
mat = [0, 1; 2, 3]
mat[0,0] => 0
mat[1,1] => 3
mat[1,0] => 2
mat[0] => 0
mat[1] => 2
mat[2] => 1
The index can be calculated from the row and column number.
index(1,0) => 1
mat[index(1,0)] => 2
Square matrices also support having the inverse and determinant found.
mat = [1, 2; 3, 4]
The symbolic inverse can also be taken but is limited to matrices whose dimension
is less than 9.
### Vectors
A vector is a single column or single row matrix. There are additional operations
to perform on vectors:
vec = [1, 2, 3]
||vec||1 => 6
||vec||2 => 3.7417
||vec||3 => 3.3019
## Ranges
Ranges represent lists of integers and are specified using minimum and maximum
values, inclusive.
For example, we might measure the side of a square room as 10 meters with a
measurement error of 10 cm. This can be represented as the range:
We can see that the area is imprecise: it is anywhere from `99` to `101` meters
squared.
Map can also be used to combine multiple lists by passing them to a function of two
parameters.
Reduce takes an optional third argument that specifies the initial value of the
accumulator.
To make using `reduce` simple, here is how you can express the sum and prod
functions in terms of it:
## Logic Programming
And:
Or:
Not:
### Comparison
Less than:
Greater than:
Equality:
3 == 3 => true
2 == 3 => false
Inequality:
3 != 3 => false
2 != 3 => true
2 ≠ 3 => true
The `if condition then value else other value` expression is used to give alternate
values depending on some logic:
## Computer Numbers
Here's a little function to get integral hexadecimal color values in the range [0,
255] from numbers in the range [0, 1].
color = round(f*0xFF)
You can use the `&` operator to do bitwise AND operations, and the `|` operator for
bitwise OR operations.
Don't confuse & and | with the logical && and || operators:
## Complex Numbers
1 + 2i => 2i + 1
i*i => -1
(1+2i)*i => i - 2
i^43 => -i
## Derivatives
You can take the derivative a function with respect to a variable using the `der`
function:
projectile(t) = 1/2*a*t^2 + v0*t + x0
You can even specify a derivative to the nth degree using a third argument to
`der`.
der(projectile, t, 2) => a
func = 3 + |a - 2|
der(func) => if a < 2 then -1 else 1
You can also use the `∂` symbol as a synonym for `der`:
∂(x^2, x) => 2 x
## Standard Library
### abs
The absolute value of an expression can be specified with the `abs` function or by
surrounding the expression with bars `|`:
|-4| => 4
|-x| => |x|
abs(-4) => 4
abs(-x) => |x|
choose(8, 3) => 56
nCr(8, 3) => 56
### conj
These are the standard circular trigonometry functions. Their arguments are in
radians.
cos(0) => 1
cos(pi/3) => 0.5
cos(pi/4) => 0.7071
cos(60°) => 0.5
sin(0) => 0
sin(pi/3) => 0.866
sin(pi/4) => 0.7071
If you specify the units of the angle, Calca will convert it to radians for you.
These are the hyperbolic trigonometry functions. Their arguments are in radians.
cosh(0) => 1
cosh(pi/3) => 1.6003
cosh(pi/4) => 1.3246
sinh(0) => 0
sinh(pi/3) => 1.2494
sinh(pi/4) => 0.8687
### ceil
ceil(2.1) => 3
ceil(-2.1) => -2
### cross
### der
### dot
### filter
### floor
floor(2.1) => 2
floor(-2.1) => -3
### inv
You can specify which variables with respect to which the Jacobian should be taken
by passing them as single variable arguments:
### len
len([1,2]) => 2
len([1,2;3,4,5]) => [2, 3]
len({x:100,y:343}) => 2
len("foo") => 3
Logarithms are specified using the `log` function. It takes two args: the value and
the base of the logarithm.
### max
Finds the argument with the maximum value and returns that value.
You can also pass a matrix whose elements will be scanned for a max value.
### min
Finds the argument with the minimum value and returns that value.
You can also pass a matrix whose elements will be scanned for a min value.
### prod
The second argument names the variable to multiply and gives the range.
### round
Rounds a number to the nearest integer. It uses the standard computer odd/even
rules for numbers half way between integers.
round(2.1) => 2
round(-2.1) => -2
round(1.5) => 2
round(2.5) => 2
round(2.50001) => 3
### sign
Returns either `-1`, `0`, or `1` depending on whether the argument is negative,
zero, or positive.
sign(-100) => -1
sign(0) => 0
sign(100) => 1
### sqrt, √
sqrt(9) => 3
sqrt(a) => sqrt(a)
a^0.5 => sqrt(a)
√(9) => 3
### sum, ∑
The second argument names the variable to sum and gives the range.
sum(x*x, x=1..5) => 55
sq(v) = v^2
If only one argument is passed and it is a matrix, then its elements are summed:
### taylor
Taylor series approximate functions at a point using a polynomial. It's great for
simplifying otherwise complex or hard-to-compute mathematics.
taylor(f, x = 0, 1) => x + 1
taylor(f, x = 0, 2) => x - 0.5 x^2 + 1
taylor(f, x = 0, 3) => x - 0.5 x^2 - 0.1667 x^3 + 1
### tod
Displays a time value in hours, minutes, and seconds. This is known as the "time of
day".
### truncate
truncate(2.1) => 2
truncate(-2.1) => -2
## Glossary
**Definition** is the assignment of an expression to a name. Definitions can also
include an optional parameter list if they are meant to represent functions.
**Equation** is a set of two expressions that are set equal to each other using the
`=` operator.