## Calculations

#### Text values

Blockpad values can be text like in conventional spreadsheets (called strings in most programming languages).

##### Inputting text values

To input text in a spreadsheet or table cell, just type the text in. If your text starts with an equals sign, use an apostrophe at the beginning of the text.

This marks the cell input as text, and not as a formula. The apostrophe will not be included in the text value. ###### In a formula

To input text in a formula, use quotation marks at the beginning and end of the text you are inputting.

Both double quotation and single quotation (apostrophe) marks are acceptable. Text values specified like this can be used in the middle of equations directly, or you can use them to define named values, which you can then use in equations. ###### Escape characters

Because quotation marks specify the beginning and end of text, you can't use the same kind of mark inside of the text itself.

For example, `'Don't try it'` will give you an error, because apostrophes are specifying the text value, and one is used inside the text.

Instead, you either need to use regular quotation marks to specify the text (because they are different than the apostrophe used inside), or use the escape character \ . The escape character tells Blockpad that the character that comes next should be interpreted as text, not as anything else. As another example, for the text He said, "Let's do this!", an escape character must be used on whatever quotation style is used to specify the text. If you need to use the character \ in the text value itself, then input it twice. Once as the escape character, and then again as the text you want to use. ##### Manipulating text values
###### Concatenation

The + sign works as a concatenation operator. Use it to combine text values. ###### Text functions

For other text manipulations, use text functions. You can find a list in the text function section of the functions deep dive.

As an example, some basic text functions are used below. #### Date and time arithmetic

Date values and calculations are supported in Blockpad formulas.

##### Enter a date

To enter a date in a formula, yyyy.mm.dd is the basic syntax. However, the month or day can be typed in as one digit.

For example, the date January 3rd, 1892 is typed in as 1892.01.03 . You can also enter a date using the Date() function. Our example above would be Date(1892,1,3). ##### Enter a date and time of day

You can type in a date and time, where yyyy.mm.dd.hh.mm.ss is the syntax. With this, military time must be used for p.m. times.

Seconds, minutes, or hours can be left off the end, but year, month, and date must all be specified. Otherwise, Blockpad will see a regular number with a decimal. ##### Date and time calculations

You can perform basic calculations with date values and numbers with time units. So, a date value subtracted from a date value gives a time number. Also, a time number added to or subtracted from a date value gives a date value. #### Logic and Boolean values

Logic tests and functions in Blockpad are similar to logic operations in conventional spreadsheets. The main difference between normal spreadsheets is that two equals signs are used to test equality. Other than that, it's the same.

• == equal to
• != not equal to
• <> not equal to (equivalent to !=)
• < less than
• > greater than
• <= less than/equal to
• >= greater than/equal to
You can change math layout settings to make these operators appear more traditionally when shown in math notation (e.g. ≠, ≤, and ≥). Note that the operators in the formula are always those shown above, but you can change how they are displayed.

You can use If() to perform if/then logic calculations. If() in Blockpad works just like If() in conventional spreadsheets, but it displays as a logic table. You can also type Boolean values (true or false) directly into a formula. If multiple tests are used in a row, then all of them must be true separately to yield true. This is the same as using the And() function on each test separately. #### Matrices and arrays

Blockpad dynamic equations support arrays and matrices. Many of the built-in functions are matrix compatible as well. ##### Enter an array or matrix

To create an array in a formula, brackets are used. Brackets ([ ]) specify the start and end of the array. Commas (,) separate members of one row into columns, and semi-colons (;) specify the end of a row. For example, [11, 22, 33; 22, 33, 11; 33, 11, 22] typed into a formula produces the matrix below.

In Blockpad, arrays and references to groups of cells are treated the same. So, arrays can be created by referencing a group of cells from a spreadsheet or table. In this case, the array will update with the table.

##### Array names

Array values can have names, just like normal number and text values. ##### Matrix calculations

To add, subtract, divide, and multiply matrices, the normal operators are used (+, -, /, *). To find the inverse of a matrix, you can raise it to negative one (^-1) or use the inverse function. If a matrix calculation can't be completed, then you will get an error. Matrices also support units and unit tracking Functions work with arrays too. Some treat the array like a group of spreadsheet cells. And some treat the array as a matrix. ##### Item-by-item calculations

In addition to matrix calculations, Blockpad will do item-by-item calculations on one-dimensional arrays that are the same size. (Other names for this include elementwise calculations, vector functions, or broadcast calculations). Blockpad will only do these types of calculations if the size of the arrays match. Otherwise you will get an error.

In addition to the basic math operators (+, -, *, /, and ^), many Blockpad functions will do item-by-item calculations. ##### Arrays - more than just numbers

Arrays can have any type of values: text, numbers, even more arrays - anything that can go in a spreadsheet cell and a little more. A useful application is to use lookup functions with arrays, just like using them with a spreadsheet. Arrays can be displayed in a spreadsheet too.

###### Make an array formula:
1. Type in a formula that results in an array.
2. Press ctrl+shift+enter instead of enter to complete the formula.

Now when you try to edit any cell in the array, you are prompted to edit the array formula. However, you can still reference the cells like normal.

If there are non-empty cells that an array formula tries to fill, then the formula still runs, but a red line appears to let you know it didn't fill all of the cells. ##### Array parentheses lookup

You can reference the values inside of an array using parentheses after the array name. The basic synax is ArrayName(RowNumber, ColumnNumber). If you give just one number in the parentheses, then it specifies a row. You can specify a column by typing an asterisk for the row. This works to specify just a row too, just put an asterisk for the column. ##### Asign values using arrays

Arrays can be used to assign multiple value names in one equation. To do so, set up an array on the left side of the equals with the new value names, and set up an equal sized array on the right side of the equals with the respective values for each name. There are a number of useful functions to work with arrays, most of them in the scripts library (Library.Scripts), which isn't automatically included in Blockpad files. To access them, you need to use the long form library name, create a shortcut name, or include the library in the file.

In these examples, the scripts library is included in the file, so below are instructions for how to do so.

###### Include the scripts library:
1. In the toolbar, select Libary>Include Libraries.
2. Click Add in the window that appears.
3. Select Scripts.
4. Click Ok.
5. Click Close to exit the include libraries window.

Below we cover the following functions:

LinearSeries() Creates a one dimensional array given start, end, and interval.
Identity() Returns an indentity matrix given the size.
Each() Performs a function on each item in an array.
EachRow() Performs a function on each row in an array.
EachColumn() Performs a function on each column in an array.
Where() Returns a one dimensional array of items that meet provided criteria.
First() Returns the first item from an array that meets the provided criteria.
Last() Returns the first item from an array that meets the provided criteria.
ConcatRows() Combines arrays by vertical stacking.
ConcatColumns() Combines arrays by horizontal stacking.
ConcatItems() Combines arrays into a one-dimensional array.
###### Use LinearSeries() and Identity()

LinearSeries() is relatively straightforward. Specify the start of the array, the end of the array, and the interval of the steps between. Identity() is even easier. Just specify the size (n) of the matrix, and the function will create an n x n identity matrix. ###### Using the Each() function.

The Each() function performs a specified function on each item in an array, and then returns that as a new array. It looks like this: `Each(Array, In-line function to perform)`. In the example above, each item in array A_4 is squared. Notice the in-line function. For Each() (and many other array formulas), the in-line function follows this form `nameForItem => formula using nameForItem`. Note that you name the nameForItem, so the example below works the same as the example above. Below is another example. Notice that outside values can be used in the function, in this case Pressure. EachRow() and EachColumn() are similar to the Each() function, but they work on rows and columns instead of individual items. The set up is the same, including the use of in-line functions, but the in-line function is working on a one-dimensional array (the row or column).  By default, EachRow() and EachColumn() act on the rows/columns as arrays. Because of this, the output can be an array of arrays, as in the example below. You can also set up EachRow() and EachColumn() so that each item in the given row (or column) matches a parameter in the in-line function.

If more than one parameter is used for the in-line function, then the parameter names act as the 1st, 2nd, 3rd, ... nth members of each row. For this to work, the number of function parameters needs to exactly match the number of items in the row or column. ###### Using Where(), First(), and Last().

Where(), First(), and Last() are used to find specific values in an array, and they all follow the same pattern. First let's look at Where(), the most general.

Where() finds all of the items in an array that meet a criteria, and returns them in a one dimensional array. It looks like this: `Where(Array, Criteria function)`. The criteria function typically consists of just a boolean operator (==, >, <, <=, or >=) and a value. This is actually short hand for an in-line function (like talked about above). You can also specify criteria using the form `nameForItem => formula using nameForItem`. Where the formula has a boolean result. The examples below do the same thing as those above. Most of the time you don't need this, but it allows more functionality. In the example below, the Where() function is used to filter out non-length units. The First() and Last() functions are like the Where() function, but return a single value instead of an array. It's like if you performed the Where() function and then kept only the first (or last) value from the array. You can also use First() and Last() without the criteria. They just return the first or last value from the array. ###### Using ConcatRows(), ConcatColumns(), and ConcatItems()

ConcatRows() and ConcatColumns() are used to combine arrays by stacking the rows or columns. ConcatItems() combines arrays by putting all items into a single one-dimensional array. #### Complex numbers

Blockpad supports complex numbers in a very simple way. Simply use the letter i to denote the imaginary number inside of a formula/dynamic equation. The square root of a negative number will also produce i. 