4

I want to add/subtract two vectors, X and Y in Calc.

If I calc constant numerics within Org-mode source block, it's o.k.

#+BEGIN_SRC calc :session *Calculator*
[1,1] + [2,2] + [3,3]
#+END_SRC

#+RESULTS:
: [6, 6]

#+BEGIN_SRC calc :session *Calculator*
[3,3] - [2,2] - [1,1]
#+END_SRC

#+RESULTS:
: [0, 0]

#+BEGIN_SRC calc :session *Calculator*
[1,10,100] * [10,100,1000]
#+END_SRC

#+RESULTS:
: 101010

Applying Calc within Emacs Lisp source code block in Org-mode is o.k.

#+BEGIN_SRC emacs-lisp :results value scalar :exports both
(setq X '[1 2])
(setq Y '[3 4])
(calc-eval (format "%s+%s" X Y))
#+END_SRC

#+RESULTS:
: "[4, 6]"

On the other hands, If I try in Calc source block with variables , it fails.

#+BEGIN_SRC calc :var X=[1 2], Y=[3 4]
X+Y
#+END_SRC

# (wrong-type-argument number-or-marker-p [1 2])

Add can be done strangely with further extra brackets,

#+BEGIN_SRC calc :var X=[1 2], Y=[3 4]
[X]+[Y]
#+END_SRC

#+RESULTS:
: [[4, 6]]

but fails in subtraction.

#+BEGIN_SRC calc :var X=[1 2], Y=[3 4]
[X]-[Y]
#+END_SRC
# (wrong-type-argument number-or-marker-p [3 4])

How can I add/subtract vectors by Calc in Org-mode source code block ?

5

I'm not sure about the actual reason plus won't work the way you expect, but this is what Calc does when you add vectors interactively:

#+BEGIN_SRC calc :var X=[1 2], Y=[3 4]
  map(add, X, Y)
#+END_SRC

The result where you get [[4, 6]] seems to indicate that Calc needs vectors to be 2D matrices with a single column, rather than 1D matrices.

  • I thank you for your help! Thank you for your great answer ! – RUserPassingBy Jun 25 '15 at 8:31
  • @RUserPassingBy sure, you are welcome! :) – wvxvw Jun 25 '15 at 12:23
  • where does this map function comes from? is it a calc function? How do I call it from a calc buffer? – Jenia Ivanov Aug 8 '15 at 13:03
  • @JeniaIvanov this would be V M if you wanted to call this interactively (it will prompt you for a function to map with. – wvxvw Aug 8 '15 at 22:33
  • @wvxvw thanks a lot. How do I apply arbitrary calc command in a src block? #+BEGIN_SRC calc [1, 2] [2,3] '+ #+END_SRC Doesn't work. It work if the two values on the stack are numbers though. – Jenia Ivanov Aug 10 '15 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.