Skip to content

tamgucalc (en)

Claude Roux edited this page Jul 3, 2020 · 32 revisions

A Spreadsheet in Character Mode With Lisp Formulas: TAMGUCALC

Version française

This extremely lightweight spreadsheet launches instantly into your terminal. It allows you to enter numbers, define labels, and most importantly, to enter numerical formulas in Lisp. You can then save your spreadsheets with their formulas in a CSV file. You can also export your worksheets, keeping only the raw data.

The code is less than 2000 lines long and is entirely modifiable at your convenience, if you want to add your own options.

The improved version adds new basic functions (see below) to make your spreadsheet even more powerful.

Initialization

By default, the spreadsheet uses the dimensions of your terminal to define the number of rows and columns. The size of a column is 10 by default. All of these dimensions can be changed within the spreadsheet. Note that when you change these dimensions, they are saved in your worksheet.

Enter a value

To enter a value, simply position the cursor on a cell with the arrows and enter your value. You can then move to the next cell with the arrows or press the "enter" key. A cell can contain numbers, formulas or labels.

You can also use the mouse to position the cursor on a particular cell or to select a group of cells.

Enter a Lisp formula

To enter a formula, simply position on a cell and type: '('. When your formula is complete, the system checks that the parentheses balance and displays the message: "Ready to compute".

Just press: "enter" and it will be calculated.

Selecting a cell

When you type a formula, you can use the arrows to select the cells that go into your formula. When you have selected a cell, press "enter" to save it in your formula.

Selecting a cell range

You can use the mouse to select a range of cells or use the keyboard.

To choose a cell range, you must first choose a first cell, then type ".." (two points) and then choose a second cell. tamgucalc then replaces your selection automatically with the following rules:

The choice of a cell appears in the following form:

  • mat[i:j]: a single cell at positions i,j.
  • mat[:c][a:b]: 'c' is the column number, 'a,b' are the rows from 'a' to 'b' in column 'c'.
  • mat[r:][a:b]: 'r' is the row number, 'a,b' are the columns from 'a' to 'b' in row 'r'.

Note, the use of "r:" and ":c" to identify rows and columns.

Multiple rows and columns

Note that when you define a range that includes several rows and columns, tamgucalc introduces an "&&&" operator to merge all the selected rows into a single data vector. In this way, your selection becomes a single element.

Example:

(sum (&&& mat[1:][1:3] mat[2:][1:3])

Formulas

Formulas in tamgucalc must be written in the Lisp defined for Tamgu (see documentation). You can define lambdas functions, functions (defun) or simply use the basic operators.

  • By default, all numerical values are "double" in the C direction (float type for tamgu).

Operators

Traditional operators such as: +,-,/, * are of course available.

You can also use: "sum" and "product". Note that "+" is used for atomic values and that "sum" applies to both lists and atomic values.

The set of Lisp methods defined in Tamgu is also available, as well as the methods available for lists, such as "size" for example.

# addition
(+ mat[6:1] mat[7:1] mat[10:1] ) 

# Rest of a division
(% mat[6:1] mat[7:1])

# logarithm Nepean
(ln mat[9:1] )

# You can encapsulate your formulas
(* 2 (+ mat[6:1] mat[7:1] mat[10:1]))

# To test a cell content
(if (>= mat[8:2] 60) 20 0)

# Apply a change to a list and add it up #
(sum (_map '(* 2) mat[:1][6:10])) 

# Filter the values on which to apply our sum
(sum (_filter '(> 20) mat[:1][6:10])) 

Lambdas

You can also define lambda functions in your cells and perform any calculation you want including recursions.

# A simple function to calculate the average of a list
((lambda (x) (/ (sum x) (size x))) mat[1:][1:10])

# You can also implement recursions

( lambda (x) 
     (if (eq x ()) 
         0 
         (+ 
            (* 2 (car x)) 
            (self (cdr (cdr x)))) 
  mat[:1][6:10])

Functions

When you define a function with "defun" in a cell such as:

(defun avg(x) (/ (sum x) (size x))

The cell then takes the name of this function. You can now use it in your sheet as a new operator:

(avg mat[:1][1:8]) 

Regular Formulas

It is also possible to enter regular formulas by starting your definition with an "=" and ending with a ";".

= mat[1:2] + mat[2:4] - ln(mat[3:4]);

Complex Values

Some values are available such as: pi, _phi, _tau and _e. You can use them directly in your code. Note that when you type one of these values in a formula, they are replaced by their Greek name.

Additional functions

Functions can also be defined directly in tamgucalc code and then used in your sheets. Note that if you want to share your leaves, you will need to share these functions as well. A number of them has already been added as examples:

(average selection)

average calculates the average of a selection: (average mat[:1][1:10])

(fillcolumn selection cell_i cell_j)

fillcolumn fills the column starting at cell_i, cell_j with the contents of selection.

(fillcolumn (spell mat[:1][1:10] false) 1 2) fills column 2, starting from row 1 with the sorted data of the selection.
(fillcolumn (cauchy_distribution 20 0 1) 1 3) fills column 3 with a Cauchy distribution (see chapter 37: Random for more)
(fillcolumn (poisson_distribution 20  1) 1 4) fills column 4 with a Poisson distribution 

(fillrow selection cell_i cell_j)

fillrow fills the line starting at cell_i, cell_j with the contents of the selection.

(fillrow (sort mat[:1][1:10] false) 1 2) fills row 1, from column 2 with the sorted data of the selection.

(upto selection val)

upto retrieves the sub-list in selection up to the value val.

(fillcolumn (sort (upto mat[:1][10:] 0) false) 1 2): fills column 2 with the list of values in column 1 up to the first 0. 

Note the use of mat[:1][10:] to extract the full column from cell 10,1.

Basic commands

tamgucalc offers some options:

  • Control+b: Black Mode (fom black background terminals) also -b on the commande line
  • Control+i: Display/Hide indexes also -i on the command line
  • Control+n: Display/Hide messages also -n on the command line
  • Control+e: edit a formula
  • Control+d: delete cells
  • Control+k: copy a group of cells
  • Control+x: move a group of cells
  • Control+g: goto a cell. Separate row from column with a ",": r,c
  • Control+s: Save
  • Control+w: Save as
  • Control+f: Save raw data.
  • Control+r: allows you to change the number of rows and columns displayed on screen (r:c)
  • Control+t: allows you to change the width of a column
  • Control+q: Quit

Install tamgucalc

You can find "tamgucalc" at: tamgucalc.tmg

A more advanced version with mouse control is available here: tamgucalc_mc.tmg

To run "tamgucalc" you must also get the "tamgu" interpreter, precompiled versions of which are available at:

To launch tamgucalc: tamgu tamgucalc.tmg (file)

Note that file is optional...

Clone this wiki locally