New version 5.6!

We are proud to present the new version 5.6 of the Calcpad software. It is much faster than the previous versions and includes a lot of new features.

Calcpad is ported to the latest Microsoft .NET 6.0 platform, which is faster than .NET Framework 4.x we used before. On the other side, the Calcpad code was significantly optimized. Now, we use expression trees to pre-compile parts of code that are repeatedly executed – bodies of functions, methods etc.

We also implemented better algorithms for numerical analysis. For example, we replaced the adaptive Simpson’s method for numerical integration with the adaptive Lobatto quadrature proposed by Gander and Gautschi. We adopted this algorithm for the C# language. We also developed an improved version of Anderson and Bjorks‘s root finding algorithm. That is why Calcpad 5.6 runs 3-4 times faster than 3.x versions in general and over 20 times faster when numerical methods are involved.

For plotting, we introduced adaptive interpolation of functions. It provides smooth graphs with the minimum number of calculation points. They are distributed unequally, depending on the function curvature as shown on the following figure.

Output formatting was also improved in respect to variable substitution and units of measurement. Speed of units arithmetic was also enhanced by optimizing memory consumption. That is how we reduced the pressure on the Garbage Collector.

There are also some improvements in the UI layer. Recent file list was introduced in version 5.0. Behaviour of the Undo function was improved. Support for Open Office or Libre Office was added alternatively to MS Word. Thanks to the wkhtmltopdf project, now we have direct export to pdf.

And last, but not at least, we added a lot of new and useful functions as follows:

Since version 5.0:

  • new factorial operator “!”;
  • log_2(x) – binary logarithm function;
  • cbrt (x) – cubic root;
  • root(x; n) – n-th root;
  • csc(x) – cosecant;
  • sec(x) – secant;
  • csch(x) – hyperbolic cosecant;
  • sech(x) – hyperbolic secant;
  • acsc(x) – inverse cosecant;
  • asec(x) – inverse secant;
  • acsch(x) – inverse hyperbolic cosecant;
  • asech(x) – inverse hyperbolic secant;
  • acoth(x) – inverse hyperbolic cotangent;

Since version 5.6:

  • min(x; y; z…) – minimum of multiple values;
  • max(x; y; z…) – maximum of multiple values;
  • sum(x; y; z…) – sum of multiple values;
  • sumsq(x; y; z…) – sum of squares;
  • srss(x; y; z…) – square root of sum of squares;
  • average(x; y; z…) – average of multiple values;
  • product(x; y; z…) – product of multiple values;
  • mean(x; y; z…) – geometric mean;
  • switch(<cond1>; <value1>; <cond2>; <value2>; …; <default>) – selective evaluation;
  • take(n; a; b; c…) – returns the n-th element from the list;
  • line(x; a; b; c…) – linear interpolation for variable x;
  • spline(x; a; b; c…) – Hermite spline interpolation for variable x;

You can use the “take” function to represent vector and matrix-like behaviour. With the “line” and “spline“, you can interpolate over tabular data. Double interpolation is also possible with just a few line of code. If you have a function that is slow and heavy to calculate, you can use spline interpolation to speed up you program. Just sample the function in a few points and interpolate among them. We will discuss this in the next posts.

You can download the latest version from the following link:

https://calcpad.eu/download/calcpad-setup-en-x64.zip

Enjoy!

Calcpad is free and open source!

In December, 2021 Calcpad joined the big family of open source software. It is now distributed under the MIT License. The source code is available on both SourceForge and GitHub.

We welcome any contributors that could help improving the software or developing more useful features. The next big challenge is to add vectors and matrices.

Even more 3D effects

In the last version I improved the 3D plot shading and added some nice 3D effects. Bellow is the plot of the function

f(x; y) = cos(x) + cos(y) + cos(x·y/3)/2 + cos(√(x2 + y2)·2)/2

with the “Rainbow” color scale and the “Shadows” option on:

Specular

What is new is the specular light effect (the little shiny reflections). I also replaced the Gouraud shading procedure with the Phong one. It improved the image quality  without any noticeable impact on the speed (surprisingly). Since the light is distant and the view is isometric, I use the Blinn–Phong shading model. Instead of calculating the reflection vector for each vertex, it calculates the halfway vector between the viewer and light-source. It is performed once for the whole model, and that is why it is faster.

In general, there is additional overhead for generating the Html output, converting the image to base64 and rendering the Html into the “Output” window. However, as long as it takes much less than a second for all of it, the speed is not of a big concern.

Math Art with Calcpad

Most people consider math boring, but sometimes it can be really beautiful. Recently I found some unusual application of Calcpad – to draw nice pictures using math formulas. In general, you can plot functions of two variables using the $Map command:

$Map{f(x;y) @ x = a : b & y = c : d}

I notices that you can get nice effects using the following types of functions:

f(x;y) = abs(cos(p1(x;y)) + cos(p2(x;y)) ...)

It creates families of intersecting curves that from canyons with hills in between. If you select “none” for the color scale, it creates nice and smooth pictures with shadows only. Bellow are some examples:

circles-and-hyperbolas
f(x;y) = abs(cos(x^2/10) + cos(y^2/10))

unsymmetric-hyperbolas
f(x;y) = abs(cos(x + y/3) + cos(x*y/7))

zig-zag-tiles
f(x;y) = abs(cos(x – sin(y)) + cos(y – sin(x)))

concentric-waves
f(x;y) = abs(cos(r(x;y)*e^(ξ*r(x;y)))*e^(-1.5*ξ*r(x;y)))

intersecting-parabolas
f(x;y) = abs(cos(r(x;y)) + cos(y))

intersecting-parabolas-x
f(x;y) = abs(cos(x) + cos((y/4)^2))

puzzle
f(x;y) = abs(cos(2*r(x;y)) + cos(x) + cos(y))

buttons
f(x;y) = abs(cos(2*r(cos(x/2);cos(y/2))))

pins
f(x;y) = abs(r(cos(x/2);cos(y/2)))

In the above equations, r(x;y) = sqr(x^2 + y^2).

Download the latest Calcpad 3.2 and try to create your own pictures. Enjoy!

Plotting with Calcpad 3.2

The new and enhanced version of Calcpad 3.2 was released. Besides other improvements, we added some nice visual effects for 2D plotting.

In general, you can use Calcpad to plot functions of two parameters f(x; y), by the $Map command:

$Map{f(x;y) @ x = a : b & y = c : d}

The result is displayed as a 2D contour map. For example, let us take the function:

f(x; y) = cos(x) + cos(y) + cos(π/2*sqr(x^2 + y^2))

The $Map command will produce the following image:

The left one is without shadow effects. Different values of the function are represented by the respective colors. Blues are the lowest and reds are the highest. However, you need to have quite a rich imagination to figure out how the surface actually looks. That is why we added a 3D shadows effects. On the right side, you can see the same image, but with shadows. The light here comes from North-West direction. Now it looks much more natural and it is easier for the human eye to perceive the shape.

The shading is calculated using the dot product of the normal vector to the surface by the vector of the light.

There are also other plotting options available:

Toolbar

You can select among different color scales:

If you select “smooth” (gradient) scale, you will get the following image:

Gradient map with shadows

Instead of having contours, now the colors flow smoothly from one to another. That is because the color of each pixel is calculated individually based on its value.

Download the latest version of Calcpad 3.2 and try by yourself:

https://calcpad.eu/download/calcpad-setup-en-x64.zip

Non-metric units in Calcpad

Calcpad version 3.0 already supports some non-metric units, that are commonly used in science, technology and engineering. A list of currently supported units is available in Units section of the User manual. More units can be easily appended in the next version. We will appreciate your suggestions very much.

You can download the latest version from the following link:

https://calcpad.eu/download/calcpad-setup-en-x64.zip

Calcpad supports both Imperal and USCS system. Some of the units have slightly different values although their names are the same. You can select which system to use by switching the respective option in the program window (●Imperial/○US).

You can convert between metric and non-metric units. That is possible because both systems exist simultaneously inside the Calcpad core engine. You can also mix them in one and the same expression. Open Calcpad on your desktop or go to the online calculator and try the following examples:

2ft + 3in|cm

kg/lb

You will get the following answer in the output window:

2ft + 3in = 68.58cm

kg/lb = 2.204623

Converting temperatures is a little bit more tricky because the origins of the different temperature scales are not the same. For example: 0°C = 32°F. Because of that, it makes a difference whether you convert between absolute values or temperature differences. In the next post, we will discuss more about this and how this problem is solved in Calcpad.

Content folding in Calcpad

We already know how to hide contents completely in the calculation notes, using output control (#hide-#pre-#post-#show) or Html comments. We already discussed that in the publication: Quadratic equation – part II. However, this is not always a good option. Lets have a look at the online calculation sheet for SLS design of RC beam:

https://calcpad.eu/Worksheet/71/sls-design-of-beam-with-rectangular-section

Click “Calculate” to see the results:

sls beam

The required calculations for one beam are so many, that it would take about 6 pages to print them all. That is why, most of the details are folded inside the headings:

Concrete creep and shrinkage … ▼

You can always unfold them by clicking on the respective heading in case you need to check something. The content will appear and the heading will change as follows:

Concrete creep and shrinkage ▲

If you click again, the content will fold back. This is an excellent option. You are not forced to print all details for each beam, but only the most important information. In the same time, everything is available when you need to check something. But how it’s made in Calcpad?

Actually, it is pretty easy to do this with just a bit of Html. Just add the following code around the content to be folded:

'<div class="fold">
'<h4>Heading</h4>
'Content to be folded
'</div>

In your code, replace “Heading” and “Content to be folded” with the actual text. It is so simple, because most of the work is done preliminarily. Each calculation note starts from an empty Html template, which already contains the required CSS and jQuery code. The template is located in the program directory and it is named “template.html” (obviously). If you are familiar with HTML, CSS and JS/jQuery, you can add some other cool features for your worksheets.

How units work?

In this post, we will take a quick look inside Calcpad and see how units actually work. But first, let’s make a brief overview of the theoretical basis.

Theoretical basis

There are only seven base units in the whole universe. They correspond to the seven physical dimensions:Measurement

  • mass – kilogram (kg)
  • length – meter (m)
  • time – second (s)
  • electric current – ampere (A)
  • temperature – Kelvin (K)
  • amount of substance – mole (mol)
  • luminous intensity – candela (cd)

All other units are derived from the base ones, using the respective laws of physics. For example, forces are measured in Newtons (N). By definition, force is mass × acceleration, so 1 N = 1 kg·m/s2. Such units are called “composite”. They can be always decomposed to their constituent base units.

Other method to derive units is by adding prefixes. I this way, we can obtain multiples of both base and composite units. For example,  kilonewtons – kN = 103·N, meganewtons – MN = kN = 106·N and so on. All possible prefixes are listed in the following table:

Name deca hecto kilo mega giga tera peta exa zetta yotta
Symbol da h k M G T P E Z Y
Factor 101 102 103 106 109 1012 1015 1018 1021 1024
Name deci centi milli micro nano pico femto atto zepto yocto
Symbol d c m μ n p f a z y
Factor 10−1 10−2 10−3 10−6 10−9 10−12 10−15 10−18 10−21 10−24

However, not all prefixes are commonly used for all units, and that is why they are not included in Calcpad. Full list of the available units is provided in the user manual:

https://calcpad.eu/help/27/units

Internal implementation

Any software implementation must allow the units to be compared, converted and scaled. For that purpose, we need to store the information for how units are composed by the base ones. We also need the corresponding scale factors, in order to allow for multiples. Then, we have to define all possible operations like comparison, addition, subtraction, multiplication, division and exponentiation.

To store all this information, we need a special type of data called “structure”. It can contain multiple values of different types in a single variable. In the case of units, we will include a name and two vectors. The first one will store the exponents for all seven physical dimensions (mass, length, time, current, temp, substance, luminosity) and the second one – the respective scaling factors:

Unit = {name: "name", exponents: [p1, p2, p3, p4, p5, p6, p7], factors: [f1, f2, f3, f4, f5, f6, f7]}

A base unit can include only one value of “1” for the respective dimension. All of the rest ones must be exactly zero “0”. Composite units can have several nonzero values for the corresponding dimensions. Some examples of unit definitions are given bellow:

Base units:

Kilogram = {name: "kg", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]}
Meter = {name: "m", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}
Second = {name: "s", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

Composite units:

Newton = {name: "N", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}
Pascal = {name: "Pa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

Multiples:

Ton = {name: "t", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]}
Millimeter = {name: "mm", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 10-3, 1, 1, 1, 1, 1]}
Minute = {name: "min", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 60, 1, 1, 1, 1]}
Kilonewton = {name: "kN", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]}
Megapascal = {name: "MPa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]}

Units consistency

You can compare, add or subtract only values, which units are consistent. For example, “kg + m” does not make any sense, because both operands represent different physical dimensions. The expression “m2 + m3” does not make sense as well, because you are trying to add area to volume. However, “m + mm” is possible, because both represent length and we can be converted to each other. So, units are consistent only if they are of the same dimensions and with the same exponents. What we need to do inside the software, is to check if the exponents vectors match exactly.

Comparison, addition and subtraction

Even if consistent, units are not necessarily equal, because they can have different scaling factors. So, we still cannot operate directly with the respective values. First, we have to convert all values to the same units. For example:

3 dm + 2 cm = 3 dm + 0.2 dm = (3 + 0.2) dm = 3.2 dm

Subtraction and comparison are performed in the same way as the addition. The following algorithm can be used:

  1. Check if the units are consistent (compare the exponent vectors). If not, report an error. Otherwise, continue to step 2.
  2. Compare the scale factors. If different, compute the conversion factor for the second unit and use it to multiply the second value.
  3. Perform the operation and return the result with the units of the first value.

Multiplication, division and exponentiation

Unlike the previous ones, these operations do not require the units to be consistent. Actually, this are the ways we use to derive new composite units. In the software, these operations are performed by manipulation of the respective exponents as follows:

  1. Multiplication is performed by addition of the exponents:
    m·m2 = m(1 + 2) = m3
  2. Division is equivalent to exponent subtraction:
    N/m2 = kg·m·s-2/m2 = kg·m(1 - 2)·s-2 = kg·m-1·s-2 = Pa
  3. Exponentiation is performed by multiplication of the old and new exponent:
    (m2)3 = m2·3 = m6

The following algorithm is applied:

  1. Check if there are different scale factors and compute the conversion factor.
  2. Perform the required operation (multiplication, division …) with the units and obtain the resulting units.
  3. Perform the operation with the values, apply the conversion factor and return the result in the calculated units.

Target units

If you do not specify any target units, the result will be decomposed to the base units. For example:

100N·10m = 1000 kg·m2/s2

That is because the program cannot understand the physical meaning of your formulas. Sometimes, the result can be ambiguous and the program will not know what to do. For the above expression:

  • If it is bending moment or torque, the result will be in Nm.
  • If it is work or energy, the result will be in Joules.

In such cases, you have to specify the target units and if they are compatible, the result will be converted automatically. Always write the target units at the end of the expression, separated by a vertical bar “|”:

500N·20m|kJ = 10kJ

2000N·5m|kNm = 10kNm

Arithmetic operations with units cost extra computational time than simple numbers. However, Calcpad uses highly optimized arithmetic code that performs very efficiently and much faster than other similar software.

Calculating with units

Units

Unit conversion is one of the most tedious jobs when doing calculations. Although not very difficult, it is repetitive and time consuming.

For example, lets calculate the stress in a steel beam. Assume that span length is in meters, section dimensions are in mm, geometrical properties are in cm2 (cm3, cm4), section forces are in kN (kNm) and stress is in MPa. That requires you to include a lot of conversion factors in your formulas. So, it would be very helpful if you could write the units next to the respective values and let the software do all the conversions for you.

The most recent version Calcpad Pro 3 supports physical units in calculations. For now, only metric units (SI compatible) are included, but more units will be added in the next versions.

You can attach units to every value in an expression and obtain the result in the specified target units. The target units are added at the end, separated by a vertical bar “|”.  , Open the online calculator and try the following expression by yourself:

2m + 50 cm + 200 mm|dm

Since the target units are “dm“, the answer is 27dm (not just adding 20 + 50 + 200 = 252). That is because all values are converted automatically to the same units before the addition: 20dm + 5dm + 2dm = 27dm. If you do not specify any target units, the units of the first operand will be used.

If you want to do simple unit conversion, just write the value, followed by the source units, a vertical bar and the target units:

25m/s|km/h

The result is 90km/h, which is correct.

You can also attach units to variables and functions. If you have units in an expression that defines a variable, the result will be stored in the variable, together with the calculated units. If you specify target units at the end, they will be used instead. From this point further, the variable will be substituted always with the internally stored units. You can try the following examples:

Input Output
Example 1: Speed
'Distance -'S = 50m
'Time -'t = 2s
'Speed -'V = S/t|km/h
Distance – S = 50m
Time – t = 2s
Speed – V = S/t = 50m/2s = 90km/h
Example 2: Force
'Mass - 'M = 500t
'Acceleration - 'a = 4m/s^2
'Force -'F = M*a|kN
Mass – M = 500t
Acceleration – a = 4m/s2
Force – F = M·a = 500t·4m/s2 = 2000kN
Example 3: Stress in column
'Column load -'F = 2000kN
'Section size -'b = 500mm
'Section area -'A = b^2|cm^2
'Stress -'σ = F/A|MPa
Column load – F = 2000kN
Section size – b = 500mm
Area – A = b2 = (500mm)2 = 2500cm2
Stress – σ = F/A = 2000kN/2500cm2 = 8MPa

If you specify target units after a function definition, they will be attached permanently to this function. The result will be converted to them whenever the function is used further. However, you have to be careful to provide the proper units for the arguments. Otherwise, you can get an error.

Although Excel is still the most popular platform for engineering spreadsheets, it lacks such advantages as automated units. I think that math software is more appropriate for engineers in general, but this will be a topic for future discussions.

Read the next post to see how do units actually work. We will lift the cover and have a quick look inside the Calcpad engine, together.

Quadratic equation – part II

In the previous post we created a simple program to solve a quadratic equation. We used the well known method from school:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
'Discriminant - ' D = b^2 - 4*a*c
x_1 = (-b - sqr(D))/(2*a)
x_2 = (-b + sqr(D))/(2*a)

In this post we will make our program more professional and add some advanced features. Currently, it is fine for personal use, but if we want to give it to other users, we need to work a little bit harder.

For example, let’s try to enter a = 0 and see what happens. In fact, it will turn into a linear equation, which solution is x = -c/b. However, our program will end up with error due to division by zero. Calcpad will show -∞, +∞ or undefined, if we have 0/0 as it will happen for x_2. It is similar for the case of a negative discriminant D < 0 (unless we use complex numbers).

This is not very good. Every professional program must work correctly for any possible input data. It should either provide a sensible result or a comprehensible message to the user. But how to do this with Calcpad?

The good news is that Calcpad supports boolean expressions and program flow control as follows:

#if
  <code>
#else if
  <code>
  ...
#else
  <code>
#end if

This is a very powerful feature. It allows you to split the solution entirely, depending on some intermediate values. It is not just about using different formulas. You can even display different text and images into the output. It is very difficult to do this with Excel.

Dots “...” mean that you can add as many conditions as you need (do not place the dots into the code) Instead of <condition> you can put any expression that compares values, e.g. a ≡ 0 or D < 0. So, lets add some logic into our program as follows:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
#if a ≡ 0
  'The coefficient a is equal to zero. The equation is linear:
  x = -c/b
#else
  'Discriminant - ' D = b^2 - 4*a*c
  #if D ≥ 0
    'The discriminant is greater or equal to zero. Real roots exist.
    q = -0.5*(b + sign(b)*sqr(D))
    x_1 = q/a
    x_2 = c/q
  #else
    'The discriminant is less than zero. There are no real roots.
  #end if
#end if

Now, the program will display the correct answer in any case. You can copy the above script into Calcpad and try for a = 0 or b = 5. We also made another improvement: The classical formula x1,2 = (-b ± sqr(D))/(2·a) is numerically unstable. The answer will be inaccurate due to the approximate representation of floating point numbers by the computer. The new formula solves this problem.

So far, so good. We managed to improve our program, but it is still not very convenient for other people to use it. When the program gets larger and more complicated, it becomes difficult for users to work directly with the source code and find where to change the input values. There is also a risk to accidentally damage some formulas and produce incorrect results.

That is why, Calcpad provides a quick way to generate input forms directly from the source code. All you need is to put question marks ? wherever you want to enter values. In our program, we need to change the third line as follows:

a = ? ', ' b = ? ', ' c = ?

Then press the “form” button (left to the orange arrow) and you will get the input form:

Input form

But how we made the solution details to disappear during the input? In Calcpad, you can select which part of the output will be visible at each stage by using special keywords:

  • #hide – always hide the content after this place ;
  • #pre   – show the content only in input mode (before calculations);
  • #post – show the content only in output mode (after calculations);
  • #show – always show the remaining content (cancel);

If you want to hide all calculation details during the input, just add #post after the third line. There is also another method for hiding. Since all the output is finally formatted as Html, you can include Html tags and CSS in Calcpad comments. You can use this to enclose some contents in Html comments and it will hide as well:

'<!--''-->

Do not use this to hide sensitive information because it can be seen in the Html source. For that purpose, we strongly recommend to use #hide. It entirely skips the content in the output Html.

So, our first program is ready. It is pretty much like the one on the official Calcpad website:

https://calcpad.eu/Worksheet/2/quadratic-equation

The only additional thing is the function graph, but I am going to write an whole new post about graphing. Bellow is the final source code for the quadratic equation program. You can copy it an use it freely:

"Quadratic equation:
'a·x<sup>2</sup> + b·x + c = 0
a = ? ', ' b = ? ', ' c = ?
#post
#if a ≡ 0
  'The coefficient a is equal to zero. The equation is linear:
  x = -c/b
#else
  'Discriminant - ' D = b^2 - 4*a*c
  #if D ≥ 0
    'The discriminant is greater or equal to zero. Real roots exist.
    q = -0.5*(b + sign(b)*sqr(D))
    x_1 = q/a
    x_2 = c/q
  #else
    'The discriminant is less than zero. There are no real roots.
  #end if
#end if