(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
- 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)’
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])
Displaying all calc units in a buffer can be obtained by executing
Calc preserves units and variables in table operations.
distance | time | speed |
---|---|---|
3 km | 2.5 hr | 1.2 km / hr |
speed | simplified speed |
---|---|
40km / 2.5hr | 16. 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.
km | ft |
---|---|
2.5km | 8202.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))
km | ft |
---|---|
2.5 km | 8202.0997 ft |
Using the units from the table header:
km | ft |
---|---|
2.5 | 8202.0997 |
The same without a user’s defmath:
km | ft |
---|---|
2.5 | 8202.0997 |
Unit | Definition |
---|---|
km | #ERROR |
- Emacs Info Manual: Calling Calc from your programs
- nice blog post on RSA cryptography using emacs Calc by Chris
Wellons on his nullprogram blog. Contains examples on
calc-eval
usage.
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" "___"))
push
pushes the element onto the stackpop
deletes as many elements from the stack as the preceding integer argument indicates0 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)
(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"
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)))
- 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 => 8 $ - modulo:
$mod(10, 3) => 1$ $10 % 3 => 1 $ - binomial coefficient:
$choose(3, 2) => 3$ - random numbers:
$random(10) => 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) => 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) => 0.34457825839$ -
http://www-zeuthen.desy.de/~kolanosk/smd_ss02/skripte/
$now(0) => <11:03:18pm Sun Aug 11, 2013>$ $unixtime(now(0)) => 1376262280$
-
http://www-zeuthen.desy.de/~kolanosk/smd_ss02/skripte/