Skip to content

Instantly share code, notes, and snippets.

@rebcabin
Created April 7, 2017 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebcabin/37d7c499a55e2bdb40960c54f529e6d4 to your computer and use it in GitHub Desktop.
Save rebcabin/37d7c499a55e2bdb40960c54f529e6d4 to your computer and use it in GitHub Desktop.

calc examples

1 Version information

(princ (concat (format "Emacs version: %s\n" (emacs-version))
               (format "org version: %s\n" (org-version))))
Emacs version: GNU Emacs 24.5.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.8)
 of 2015-05-04 on dflt1w
org version: 8.3.2

2 General

  • refer to the Calc info pages
  • Note that, unlike in usual computer notation, multiplication binds more strongly than division: `a*b/c*d’ is equivalent to `(a*b)/(c*d)’

3 babel calc

Not too useful, yet. Embedded calc certainly is better for inlining math in documents. Using Elisp to directly interacting with calc also is more powerful.

24
3
'/
  • solving an equation
    fsolve(x*2+x=4,x)
        
    x = 1.33333333333
        
  • solving a linear system of equations
    fsolve([x + y = a, x - y = b],[x,y])
        

4 calc usage in tables

4.1 Unit conversions

Displaying all calc units in a buffer can be obtained by executing

Calc preserves units and variables in table operations.

distancetimespeed
3 km2.5 hr1.2 km / hr
speedsimplified speed
40km / 2.5hr16. km / hr

We can also decide to use calc via its elisp api. To understand the following lisp formula that involves calc internal functions q.v. the Calc from lisp section.

kmft
2.5km8202.10

Defining a new calc function for unit conversion with defmath

(defmath uconv (expr target-units &optional pure)
  (math-convert-units expr target-units pure))
kmft
2.5 km8202.0997 ft

Using the units from the table header:

kmft
2.58202.0997

The same without a user’s defmath:

kmft
2.58202.0997
UnitDefinition
km#ERROR

5 Calc from lisp

5.1 basic use of calc-eval

The variables in formulas are replaced by the additional arguments. Arguments can be given as string or number.

(print (calc-eval "2^$1 - 1" nil 128))
(print (calc-eval "$1 < $2" 'pred "4000" "5000"))
(print (calc-eval "nextprime($1)" nil "100000000000000000"))

;; radix can be chosen by separating radix by # from number
(print (calc-eval "16#deadbeef"))
(print (calc-eval "2#1111"))

The second argument serves as a separator if the input string parses to a list of expressions. By default the list is printed comma-separated.

(print (calc-eval "10+5,7*3,5/2"))
(print (calc-eval "10+5,7*3,5/2" ";"))
(print (calc-eval "10+5,7*3,5/2" "___"))

5.2 Stack operations: push, pop and top

  • push pushes the element onto the stack
  • pop deletes as many elements from the stack as the preceding integer argument indicates
    • 0 pop is convenient for finding out the size of the stack
  • top retrieves the value at the indicated position of the stack
(princ (format "Size of the stack: %s\n" (calc-eval 0 'pop)))
(calc-eval "10 ft" 'push)
(calc-eval "20 ft" 'push)
(calc-eval "30 ft" 'push)
(princ (format "After 3*push: Size of the stack: %s (top element: %s)\n"
               (calc-eval 0 'pop)
               (calc-eval 1 'top)))
(princ (format "element on second level of stack: %s\n" (calc-eval 2 'top)))
(calc-eval 2 'pop)
(princ (format "After 3*push: Size of the stack: %s (top element: %s)\n"
               (calc-eval 0 'pop)
               (calc-eval 1 'top)))
(calc-eval 1 'pop)
Size of the stack: 5
After 3*push: Size of the stack: 8 (top element: 30 ft)
element on second level of stack: 20 ft
After 3*push: Size of the stack: 6 (top element: 10 ft)

5.3 executing functions on the stack

(calc-eval "10 ft" 'push)
(calc-base-units)
;; retrieve the value from the stack as a string. Note that it still stays on the stack!
(print (calc-eval 1 'top))
;; clean the value from the stack
(calc-eval 1 'pop)
"3.048 m"

It is also possible to execute Calc keyboard macros, i.e. the string is interpreted as interactive keyboard strokes in calc mode.

(calc-eval "10 ft" 'push)
;; calc keys for base unit conversion
(calc-eval "ub" 'macro)
(print (calc-eval 1 'top))
;; pop one item from stack
(calc-eval "\C-d" 'macro)
"3.048 m"

5.4 raw calc objects as input to functions

calc internal functions deal with raw calc objects. These can also be obtained through calc-eval by passing the raw as the second argument.

(calc-eval (math-convert-units (calc-eval "10 m" 'raw)
                               (calc-eval "ft" 'raw)))

6 A sample of functions that can be used in formulas

  • info:calc#Formulas
  • factorial: $6! => 720 $ also fact(6) can be used in writing
  • find: $ find([5, 6, 7, 8], 6) => 2 $
  • power: $pow(2, 3) => 8 $ $2^3 =&gt; 8 $
  • modulo: $mod(10, 3) =&gt; 1$ $10 % 3 =&gt; 1 $
  • binomial coefficient: $choose(3, 2) =&gt; 3$
  • random numbers: $random(10) =&gt; 7$
  • binomial distribution: the result (`utpb(x,n,p)’) is the probability that an event will occur X or more times out of N trials, if its probability of occurring in any given trial is P: $utpb(2, 6, 1/6) =&gt; 0.263224451304$
  • gaussian distribution with mean m and stdev s. Probability that a normal distributed random variable will exceed x: uttn(x,m,s): $utpn(0.2b, 0, 0.5) =&gt; 0.34457825839$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment