The main purpose of Calcpad is to perform calculations. That is why, everything inside the input window is assumed to be mathematical expressions, unless it is enclosed in quotes. Then it is treated as comments. By default, each expression has to be in a separate line, e.g.:
You must not add "=" at the end of each expression. This is the assignment operator that is used to assign values to variables, e.g. "a = 2". When you press the button, you will see the results in the output window:
Alternatively, you can have several expressions in a single line, but they must be separated by comments, e.g.:
'Length - ' a = 3 ' m, Width - ' b = 2*a ' m, Height - ' c = 5 ' m
Each expression can include constants (numbers), variables, operators, functions and brackets. They must be arranged properly in order to represent a valid expression. The commonly accepted mathematical notation and operator precedence is used as it is taught in school. Detailed description of the expression components is provided bellow.
You can enter separate unrelated expressions or write a complete program that solves a specific problem. You can define parameters and assign values to them. Further, you can use them to define other parameters and so on until you reach the final result. You can also add text, Html and images and create a detailed and professional-looking calculation report. You can save it in a file and use it multiple times for similar problems. This is a sample program to find the roots of a quadratic equation:
Real constants can be positive and negative integer and decimal numbers. They can include digits "0" - "9" and decimal point ".". You can also use simple fractions like "3/4", but the program will accept them as expressions (division of two numbers). You cannot define numbers in floating point format: "3.4e+6". You have to use an expression like "3.4*10^6".
All constants and variables are stored into fields of type "double-precision floating point". Their values are between -1.7976931348623157E+308 and 1.7976931348623157E+308. If a result is out of the above interval, the program returns "-∞" and "+∞, respectively". The same is in the case of division by zero, but "0/0" = "Undefined". The smallest positive number is 4.94065645841247E-324. Smaller values are rounded exactly to 0.
If you select "Complex" mode, you can calculate using complex numbers. Otherwise, real arithmetic is applied. Each complex number is represented by an ordered couple (a; bi), where "a" is a real number, and "bi" is called "imaginary". It can be written in so called algebraic form: ±a ± bi (e.g. "2 + 3i"). You can also use other forms like polar or exponential by entering the respective expressions. The imaginary unit In Calcpad is written as "1i" instead of just "i". It that way are free to define a variable named "i". The imaginary unit is a special number defined by the expression (1i)^{2} = -1.
A variable is defined by its name and value using an expression like "a = 4". The "=" symbol represents the assignment operator. On the left side, only a single variable name is allowed. On the right side you can have any valid mathematical expression, e.g. "b = a + 4". Once defined, you can use the name instead of the value further in calculations. Each variable is valid from the place of its definition to the end of the script. That is why you cannot use a variable before it is defined. This will result in error. If you redefine an existing variable with a new value, the old one will be replaced and the new one will be used further on.
Variable names can include small or capital Latin letters "a" - "z", "A" - "Z", Greek letters "α" - "ω", "Α" - "Ω" and digits. Names are case sensitive. For example "a" and "A" are different variables. A name cannot start with a digit. You can also use "," (comma) and "_" (underscore). First occurrence of underscore in a name starts a subscript. For example, "a_1_1" will be formatted as "a_{1_1}". Variables can contain both real and complex values.
The following operators are supported by the Calcpad software:
The above operators are listed in their precedence order. This is the order in which the operators are evaluated. When you have different types of operators in an expression, exponentiation is evaluated first, then division and multiplication, subtraction and addition and comparison is the last. All relational operators have equal precedence. If you need to change the order of evaluation, you can use brackets. For example "5 + 2∙3" makes "11". If the addition have to be performed first, write "(5 + 2)∙3" and you will get "7∙3 = 21". Operators with equal priority are evaluated from left to right. This is called operator associativity. For example, "3 - 2 + 1" makes "(3 - 2) + 1 = 2" and not "3 - (2 + 1) = 0". Another good example is "2∙3 / 2∙3", which makes "9" and not "1". All operators in Calcpad are left-associative (calculations are performed from left to right). There is only one problem here. Exponentiation is normally considered as right-associative. It means that calculations are performed from right to left, i.e. x^a^b is evaluated as x^{ab}. However, many calculators, programming languages and worksheet software like Excel use left associativity for exponentiation. In this case x^a^b is evaluated as x^{a·b}. If you need to have x^{ab}, you must put brackets: x^(a^b). This is how Calcpad is works and there are two reasons for that:
So, exponentiation is also left-associative in Calcpad. For example, if you enter 4^3^2 in Calcpad, you will get 4^{3·2} = 4096, and 4^(3^2) will make 4^{32} = 262144.
Relational operators can return only two values: "1" for "true" and "0" for "false". You can include them into more complex expressions. For example, you can get the greater of two numbers a and b by the expression: "a*(a ≥ b) + b*(a < b)". But you need to be careful. If you use "≤" instead of "<", for the case of a equal to b, you will get a + b, which may be not exactly what you want. For that special purpose, it is better to use the built-in function max(a; b) or conditional execution (see further in this manual). You can also simulate boolean algebra using arithmetic expressions. In such cases you can use "*" instead of logical "AND" and "+" for logical "OR". For example "(2 < 3) + (2 < 1) = 1 + 0 = 1" (true) and (2 < 3)∙(2 < 1) = 1∙0 = 0 (false). Again, you have to be careful. This is not true boolean algebra. Some expressions may evaluate to other results than 0 and 1. It depends on you to properly compose the expressions and interpret the results. Also, make sure to add brackets where needed. Arithmetic operators are with higher precedence than relational.
All operators support complex numbers except those for integer division "\", reminder "%" and comparison: "<", "≤", ">", "≥". The evaluation of complex expressions is a little bit more difficult than real ones. Bellow are provided basic rules for evaluation of the most common complex operations:
Brackets can be used in two cases: to change the order of evaluation and to enclose arguments of functions. Only round brackets are allowed: "(" and ")". The software checks if the following rules are satisfied for each expression:
Calcpad uses "intelligent" bracket insertion method for rendering of the final output. It means that brackets, which are duplicate and do not affect the order of evaluation are omitted. On the other hand, brackets have to be added at some places, although not necessary in the input. This occurs mostly with variables that contain negative or complex numbers. For example:
Calcpad includes a library with standard math functions that are ready to use:
Arguments must be enclosed in round brackets. They can be constants, variables or any valid expressions. For functions of two arguments like min and max, both arguments must be separated by semicolon ";". When arguments are out of the function domain, the result is "Undefined". Exceptions from this rule are "cot(0)" and "coth(0)", which return "+∞". Arguments of trigonometric functions can be in degrees or radians. This option can be selected from the radio buttons above the output window. It can be also defined inside the script. You have to insert a line containing only: #deg for degrees or #rad for radians.
All functions are defined in the complex domain, except min(x; y) and max(x; y). Rounding functions affect both real and imaginary part. There is also several special functions, for complex numbers only:
Each function returns only one number as a result, even if it is multivalued. Knowing the function properties, you can easily calculate the other values using additional code.
You can define your own functions, besides the library ones and use them further in the calculations. In this version, custom functions with multiple arguments are supported. They must be enclosed in brackets "(" and ")" and separated by semicolons ";". Each function must be defined, using the following format: "f(x; y; z;...) = expression", where "f" is the function name and "x", "y" and "z" are the names of the arguments. On the right side you can have any valid math expression containing constants, operators, variables and even other functions, e.g.:
Once the function is defined, you can use it further in the calculations instead of repeating the same expression. To call a function, write the name and the value of the argument in brackets, e.g. f(2). Function names must conform to the same rules as for variable names. Argument values can be any valid expressions. Function calls can be inserted in other expressions like c = 4*f(a^2 + 2) - 2*b. The life cycle of a function is from the place of definition to the end of the script. If you define a new function with the same name, the old one will be replaced. You cannot redefine a library function. For example, sin(x) = x^2 will return an error.
It is not necessary to define the variable that is used for the argument. However, if there are other variables inside the function definition, they must be defined before the first call to that function. The argument works as a local level variable for the function. It means that if a variable with the same name exists outside the function, a call to that function will not change the value of the outer variable. For example:
User defined functions can be used with complex numbers as well.
Calcpad includes several special commands for graphing and numerical methods. Their names start with "$" to be distinguished from ordinary functions. Their parameters must be enclosed with curly brackets: "{" and "}". Such is the graphing command $Plot. It can draw the graph of a function of one variable in the specified interval. It has the following format:
$Plot { y(x) @ x = a : b }
, where:
For example, if you enter: $Plot { x^2 - 5*x + 3 @ x = -1 : 6 }, you will get the following result:
The above command plots only one function of one variable at a time. There are also other formats that you can use:
$Plot { x(t) | y(t) @ t = a : b } - parametric: both coordinates are functions of a third parameter;
$Plot { y_{1}(x) & y_{2}(x) & ... @ x = a : b } - multiple: plots several functions on a single graph;
$Plot { x_{1}(t) | y_{1}(t) & x_{2}(t) | y_{2}(t) & ... @ t = a : b } - multiple parametric;
$Map { f(x; y) @ x = a : b & y = c : d } - draws a 2D color map of a 3D surface, defined by f(x; y).
The $Plot{...} function must be the first item in the line. Before it, you can have only spaces and tabs, not even comments. Otherwise, the program will return an error. Any input after the closing bracket will be ignored. Graphing supports only real numbers. You can use it also in complex mode, only if x and y are real and the function always returns real result.
You can specify the size of the plot area by defining two variables: PlotWidth and PlotHeight. The default values are PlotWidth = 400 and PlotHeight = 250.
The $Map{...} function can draw either in colors varying from blue to red or in grayscale. You can enable it by checking the "Plot in grayscale" checkbox. Some examples of different plotting methods are provided bellow:
Equation:
r(θ) = cos(5/2*θ)
$Plot { r(θ)*cos(θ) | r(θ)*sin(θ) @ θ = 0 : 6*π }
Result: "rose" curve
Equation:
y_{1}(θ) = cos(θ) - sin(θ)
y_{2}(θ) = cos(θ) + sin(θ)
$Plot { cos(θ) & y_{1}(θ) & y_{2}(θ) @ θ = 0 : π }
Result: leaf composed by three trigonometric functions
Equation:
x(θ) = sqr(θ)*cos(θ)
y(θ) = sqr(θ)*sin(θ)
$Plot { x(θ) | y(θ) & -x(θ) | -y(θ) @ θ = 0 : 3*π }
Result: double Fermat spiral
Equation:
$Map { cos(x^{2} + y^{2}) @ x = -6 : 6 & y = -6 : 6 }
Result: 2D concentric waves
Calcpad has a built in "Solver" module, which can solve more difficult problems using numerical methods. It can work only with real numbers but not complex. It includes the following functions:
$Root{ f(x) @ x = a : b}
It finds a root of an equation of type f(x) = 0. If a root exists in the specified interval [a, b], it is returned as a result. Otherwise, the program returns an error. The program uses iterative methods that "brackets" the root in sufficiently small interval. If several roots exists within the initial interval, only one will be found. In such cases, it is better to plot the function first. Then you can see where the roots are located approximately and divide the interval into separate parts, containing one root in each. Then you can call the function several times for each interval to find all roots. In some cases, you can develop an automated procedure for interval splitting by using the extremum finding functions bellow.
$Inf{ f(x) @ x = a : b}
It finds the smallest value of a function f(x) within the specified interval [a, b]. The golden section search method is used for that purpose. If the function contains just one local minimum within the interval, it will be returned as a result. Otherwise, the function will return the smaller of the values at the ends of the interval f(a) or f(b). If there are several local minimums in the initial interval, the program will return only one of them, but not necessarily the smallest one. In such cases, it is better to split the interval. The value of x where the minimum is found is stored into a variable x_{inf}. If other name for the argument is used instead of x, add "_inf" at the end of this name.
$Sup{ f(x) @ x = a : b}
It works like the minimum finding function, but it finds the greatest value instead. The value of x where the maximum is found is stored into a variable x_{sup}.
$Area{ f(x) @ x = a : b}
It calculates the value of the definite integral of a function f(x) within the specified interval [a, b]. Adaptive Simpson method is used for that purpose.
$Slope{ f(x) @ x = a}
It finds the value of the first derivative of a function f(x) at x = a. It also represents the slope of the tangent to the function at the respective point.
For all of the above commands, f(x) must be continuous and smooth, except for root finding. The latest requires the function to be continuous and to have opposite signs at the end of the interval. According to the Boltzano's theorem, at least one root must exist in this case.
Unlike the plotting function, numerical functions can be included into expressions. They return values which can be used for further calculations. For example, you can store the result into a variable:
y_{min} = $Inf{ f(x) @ x = a : b}
Like the standard functions, "x" is local for all numerical procedures and its original value is not modified after the procedure is completed.
There are several other commands that allows you to calculate the result iteratively. Unlike numerical methods, they can work with complex numbers.
$Sum{ f(k) @ k = a : b}
It sums the values of f(k) for all integers k between a and b. The values of k can only grow, so it should be satisfied that a < b. Instead of f(k) you can put any valid expression that includes k. Otherwise, it will simply sum the same value k times. For example, you can use series to calculate constants. Such is the Leibniz formula for calculation of π:
4*$Sum { (-1)^{k + 1}/(2*k - 1) @ k = 1 : 1000 } = 3.1406
You can also use series to define functions. Of course, they cannot be infinite. The number of iterations should be selected to provide the required precision of the final result. The following pattern can be applied for function approximation with Fourier series:
f(x) = a_{0}/2 + $Sum { a(k)*cos(k*x*π/l) @ k = 1 : n } + $Sum { b(k)*sin(k*x*π/l) @ k = 1 : n }
For example, we can take a straight line with equation: f(x) = x/(2*l) within the interval (0; 2*l). The integration constants are a(k) = 0 and b(k) = -1/(k*π). If we plot the Fourier approximation for n = 5, we will obtain the following result:
$Product { f(k) @ k = a : b}
It works like "Sum", but it multiplies the terms instead of adding them. For example, you can define your own factorial function:
F(n) = $Product {k @ k = 1 : n}
You can use it further to calculate binomial coefficients by the well-known formula: C(n; k) = F(n)/(F(k)*F(n - k)). However, it is much more efficient to define a special procedure that computes the coefficient directly without using three factorials:$Product{ (i + n - k)/i @ i = 1 : k}
Also, the later will not overflow together with the factorial function for greater values of n.
$Repeat { f(k) @ k = a : b }
This is a general inline iterative procedure that repeatedly calculates f(k). It can be used for sums and products instead of the above procedures, but it is not so efficient and has additional overhead. However, there are more complex expressions that can be calculated only with the "Repeat" command. Normally, such expressions will make sense if you assign the result to a variable that will be included in the next iteration. So, the following pattern is more likely to be used:
$Repeat { x = f(x; k) @ k = a : b }
For example, you can apply this command to define the Mandelbrot set in a single line:
f(z; c) = $Repeat { z = z^2 + c @ i = 1 : 100 }
You should not forget to switch on the "Complex" mode. Then you can plot the result:
$Map { abs(f(0; x + 1i*y)) @ x = -2 : 0.5 & y = -1 : 1 }
Calcpad offers a comprehensive support for physical units of measurement. In the current version, only metric units are included: SI and compatible. There are 7 basic units that correspond to the basic physical dimensions:
All other units are derivative. They are obtained by the respective laws of physics. For example, force = mass·acceleration, so Newton is obtained by N = kg·m/s^{2}. Multiples of units are also supported by adding the respective prefixes to units names. For example, kN = 10^{3}N, MN = 10^{6}N and so on.
You can attach units to numbers by simply writing the unit name just after the value, e.g. 15kg. Then, you can use them in expressions like any other values. Unit conversion is performed automatically. For example, the following expression will be evaluated as:
1.23m + 35cm + 12mm = 1.59m (and not: 1.23 + 35 + 12 = 48.23)
The result is usually obtained into the first unit in the expression. If you want to use particular units, write a vertical bar "|" followed by the target units at the end:
1.23m + 35cm + 12mm|cm
The above expression will evaluate to 159.2cm. If you simply want to convert units, just write the source and the target units, separated by a vertical bar, like: mm|cm or 10m/s|km/h.
Unit consistency is verified automatically. For example, you cannot add m and s(e.g. 6m + 2s), but you can multiply and divide them 6m/2s = 3m/s.
Arguments for trigonometric, hyperbolic, logarithmic and exponential functions, must be unitless by definition. However, you can use units an any custom defined functions, if it makes sense. You can also attach units to variables. If you specify target units in a variable or function definition, they will be stored permanently. Then, the selected units will be used further in the calculations to substitute the respective value. In the next example, speed is calculated from m/s, but it is converted to km/h:
Script | Output |
---|---|
'Distance -'s_1 = 50m 'Time -'t_1 = 2s 'Speed -'V = s_1/t_1|km/h 'What distance you will travel for't_2 = 5s'? s _2 = V*t_2|m |
Distance - s_{1} = 50m Time - t_{1} = 2s Speed - V = s_{1}/t_{1} = 50m/2s = 90km/h What distance you will travel for t_{2} = 5s ? s_{2} = V·t_{2} = 90km/h·5s = 125m |
Full list of the available units is provided below:
Metric units (SI and compatible):
Non-metric units (Imperial/US):