# How to use Calc vector functions

This discussion got me investigating Calc programming with elisp, especially at the bottom this code

``````(defmath myFact (n)
(format-number (calcFunc-fact n)))
``````

then

``````(calcFunc-myFact 33)
``````

yields `8683317618811886495518194401280000000`

...all of which works nicely from my `*scratch*`. However, I would like these functions to work, but they don't seem to. The docs say many confusing things, such as prepending `calcFunc-` and `math-`. Also I've tried `(require 'calc-ext)` and tried running `(calc-extensions)`. All of attempts to use these vector functions return `void function` errors. What am I missing?

• Here's some additional info: emacs.stackexchange.com/questions/13423/…, but to give you a better answer, could you give an example of what you tried to do? Some functions may make assumptions about the way they are called (say, having particular number of arguments on the stack etc.), maybe that's the problem? Also this: emacs.stackexchange.com/questions/8202/… – wvxvw Dec 8 '15 at 22:40
• `(math-concat [1, 2] [3, 4])` gives `(vec [1 (\, 2)] [3 (\, 4)])` but `(vec-length [1, 2, 3, 4])` and `(build-vector 1 5]` etc give void function errors. – 147pm Dec 8 '15 at 23:06
• Well, this is not how you write a vector in ELisp, `,` has a special meaning in ELisp (evaluation of the following expression in the context of a quoted expression). Thus a vector would be spelled `[1 2]`, not `[1, 2]`. – wvxvw Dec 8 '15 at 23:10
• Good, but I'm not getting past `void function`. As I said above, `calcFunc-fact` works, but `vec-length` doesn't. I can only assume I haven't loaded the proper library/package. As far as how to do a vector, I was just following the examples. – 147pm Dec 9 '15 at 0:29
• This might be just a case of outdated documentation. There is a function `calc-vlen`. – politza Dec 10 '15 at 9:13

## 2 Answers

Apparently, the documentation is bad: this per the former maintainer of calc-vec.el, Jay Belanger. His advice (besides contacting emacs-devel@gnu.org) was to simply go to the main Calc docs (e.g. this and this) and use those function names (usually the one in brackets) prefixed by `calcFunc-`. Another curiosity is you need to refer to a vector in its "internal" format, namely a list: `(vec 1 2 3)`, which then can be seen as a normal Calc vector with `calc-eval`:

``````(setq myv1 '(vec 1 2 3))
=> (vec 1 2 3)
(calc-eval myv1)
=> "[1, 2, 3]"
(calcFunc-subvec myv1 1 3)
=> (vec 1 2)
(calcFunc-vlen myv1)
=> 3
``````

but it can get ugly. . .

``````(setq myv2 '(vec 2 4 6 8))
(calcFunc-arrange myv2 1)
=> (vec (vec 2) (vec 4) (vec 6) (vec 8))
``````

requiring `calc-eval` to make it pretty

``````(calc-eval (calcFunc-arrange myv2 1))
=>"[, , , ]"
``````

why `calc-eval` does this, I don't know.

But this is not always easy. Here's an example that seems only `calc-eval`-friendly:

``````(calc-eval "deg(37@ 26' 36.42\")")
=> "37.44345"
``````

This is as close as I can get

``````(calcFunc-deg '(hms 37 26 36))
=> (float 374433333333 -10)
``````

but it won't accept `36.42` for some reasons better minds than mine might understand from this `calc-math.el` code:

``````;;; Convert A from HMS or radians to degrees.
(defun calcFunc-deg (a)   ; [R R] [Public]
(cond ((or (Math-numberp a)
(eq (car a) 'intv))
(math-with-extra-prec 2
(math-div a (math-pi-over-180))))
((eq (car a) 'hms)
(math-from-hms a 'deg))
((eq (car a) 'sdev)
(math-make-sdev (calcFunc-deg (nth 1 a))
(calcFunc-deg (nth 2 a))))
(math-expand-formulas
(math-div (math-mul 180 a) '(var pi var-pi)))
((math-infinitep a) a)
(t (list 'calcFunc-deg a))))
(put 'calcFunc-deg 'math-expandable t)
``````

. . . yes, the lengths some people go to stay in Emacs, i.e., I'm trying to follow a Coursera course that wants me to use Python, but I know a better way. . .

• Can you please file a bug report to improve the documentation? – Kaushal Modi Jan 14 '16 at 14:36

Calc accepts 37@ 26' 36.42" .

Here is the simple case of 37@ 26' 36".

``````(calc-eval "deg(37@ 26' 36\")")         ; => "37.4433333333"
``````

You can call Calc functions from Emacs Lisp by `calcFunc-deg`

``````(calcFunc-deg '(hms 37 26 36))          ; => (float 374433333333 -10)
(math-format-number (calcFunc-deg '(hms 37 26 36))) ; => "37.4433333333" as a string
(string-to-number
(math-format-number (calcFunc-deg '(hms 37 26 36)))) ; => 37.4433333333 as a number
``````

Now the case for 37@ 26' 36.42" .

As 36.42 is not an integer, it is expressed as `(float 3642 -2)` in Emacs Lisp.

Your `'(hms 37 26 36.42)` should be `'(hms 37 26 (float 3642 -2))` .

Voila!

``````(calc-eval "deg(37@ 26' 36.42\")")          ; => "37.44345"
(calcFunc-deg '(hms 37 26 (float 3642 -2))) ; => (float 3744345 -5)
(math-format-number (calcFunc-deg '(hms 37 26 (float 3642 -2)))) ; => "37.44345" as a string
(string-to-number
(math-format-number (calcFunc-deg '(hms 37 26 (float 3642 -2))))) ; => 37.44345 as a number
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.80999327,"math_prob":0.98149246,"size":2464,"snap":"2021-04-2021-17","text_gpt3_token_len":834,"char_repetition_ratio":0.1199187,"word_repetition_ratio":0.0,"special_character_ratio":0.36810064,"punctuation_ratio":0.13211009,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9968661,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-18T20:06:01Z\",\"WARC-Record-ID\":\"<urn:uuid:85ab6e31-ea0a-4cb9-a336-0aecfd67aa13>\",\"Content-Length\":\"180432\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5abbb3ed-d0aa-4ed3-b6bb-2a6c20d3f7d9>\",\"WARC-Concurrent-To\":\"<urn:uuid:e30861a5-3801-4a84-934f-d18742fd940a>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://emacs.stackexchange.com/questions/18704/how-to-use-calc-vector-functions?noredirect=1\",\"WARC-Payload-Digest\":\"sha1:7TDFZYROGT4MNMRCYG4MDGFHUUAVMFJN\",\"WARC-Block-Digest\":\"sha1:CNV564KETTFREYXZSZGTTEZCGUP7I4L2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038860318.63_warc_CC-MAIN-20210418194009-20210418224009-00415.warc.gz\"}"}