## SMath – A Free MathCAD Alternative

For engineering, physics and math user MathCAD is a great package for theoretical calculations and reports. Unfortunately MathCAD is rather expensive for students and causal users, and it also isn’t support in Linux.

SMath Studio is a free alternative that supports MathCAD files and it works in Linux.

In this blog I wanted to highlight some of my notes in using SMath Studio. Some of the key take away points are:

• SMath offers a Worksheet approach that is far superior to Excel/LibreOffice for math problems.
• Equations and matrices are shown like they were hand drawn
• Units are included in the equations, and unit conversion is automatic
• easy to read IF/THEN/ELSE, WHILE and FOR LOOP can be using within a Worksheet
• SMath programming is done visually, (as opposed to writing code like Python or Matlab/Octave).
• SMath support basic plotting but it is weak compared to other packages like Matplotlib.
• SMath is good for visual reports, but Python would be a better solution for managing large amounts of data or when statistical calculations are needed.

## Getting Started

SMath Studio is supported on Window, MacOS and Linux, see https://en.smath.com/ for installation files.

For Linux user the Mono interface will need to be loaded:

```sudo apt install mono-devel
# extract to a folder
# to run SMath Studio in Linux:
./smathstudio_desktop_mono
```

The SMath Studio has a side panel of common functions and symbols. All the functions are listed in the fx dialog.

## Freehand Calculations

Smath allow users to create complex freehand calculations using the side panel and some keyboard shortcuts. The arrow key moves the cursor to different sections of a large expression that need to be modified.

## Variables and Units

Sheets are calculated from top to bottom, so it is important to define a variable before it is used. A variable is defined with a “:=“, the equal sign (“=“) is used to show the value of a variable.

For equations where units are used a dropdown of available units is shown. The default system is in metric so the conversion is also shown. Units will appear in blue.

The system will automatically manage the conversion between units, and there are some different presentations to show the steps in the conversion, fractions, or just the numeric value.

## Show Solutions

Pages can be laid out to create clear presentations with variables that are passed to equations. Like with the units, equations can be shown with their intermediate values. Below is an example that shows a venturi flow rate calculation with the intermediate values and the conversion between Newtons (N), kilograms (kg) and meters (m) to a flow rate of cubic meters/sec.

Below is a worksheet that allows a user to play with a 2nd order equation. SMath offers some graphical widgets such as: analog sliders, dropdown lists, up/down selector and toggle buttons. For this example I used three slider widgets allow users to change the constants (a,b,c). A plot is used to visually check if the equation has any X-intercepts. The SMath solve function will find X-intercepts. A small if statement is used to check if there are any intercepts. Finally an integration function calculates the area between the limits.

## File I/O

SMath supports import/export data functions for: CSV, PDF, XLS, ODF, ODS and a few other. SQLite3 databases are also supported

Below is an example worksheet where a CSV file is imported, cleaned and then exported out to a new CSV file. The data is read in with the importData function to a matrix variable (rawdata).

A for loop iterates through each row data, and an if statement checks a row for positive values. The stack function appends valid rows of data to the cleandata matrix. The last step is to use the exportData_CSV function to write out the cleandata matrix.

## Automating SMath

The Windows version of SMath Studio offers a number of command line options that can be used to save and print worksheets. Unfortunately these options don’t exist in the Linux (Mono) version.

As a work around it’s possible to use some keyboard automation tool in Linux to offer some interesting solutions. Below is a script to:

• open SMath with a user defined worksheet
• grab the SMath window focus
• Save as PDF
• Quit SMath
```#!/usr/bin/bas
#
# auto_smath.sh - open
#
myfile="quad" ; # prefix of smath file
rm \$myfile.pdf ; # remove existing PDF

# Open smath with a set file, quiet output
./smathstudio_desktop_mono \$myfile.sm  &
sleep 3

# Set focus to SMath
wmctrl -a "SMath Studio"

# Send keystroke to save as PDF
xdotool key alt+F
sleep 1
for i in {1..4}
do
xdotool key Down
sleep 1
done
xdotool key Return
sleep 1
xdotool key Tab
sleep 1
xdotool key p
sleep 1
xdotool key Return
sleep 3 ; # Give time to save PDF before exiting

# Exit SMath
xdotool key alt+F4
echo "Done Running \$myfile.sm ... saved to \$myfile.pdf"
```

This script uses the wmctrl is get and manage the window focus, and xdotools to send keystrokes. They can be installed by:

```sudo apt install wmctrl
sudo apt install xdotool
```

## Some Final Thoughts

I wish I had SMath when I was in university it would have made things so much easier and reduced a lot of manual calculation errors.

It takes a few hours to get used to the keyboard short cuts and formatting.

A few times I forgot about the fact that sheets are calculated from top to bottom and this caused me to get some strange errors. If the software locks up use the following line to kill things:

```ps -e | grep mono | awk '{system("sudo kill " \$1 "  1>&-")'}
```