0

I want to pass in 2 arrays and have python process them. The babel block works when I put the arrays directly in but if I try to pass them in via headers, they don't appear as arrays to numpy. My example follows for the failure is below.

#+NAME: p-list
[0.2757333815097, -0.0010721227154135, 0.0002933239156845]
#+NAME: q-list
[0.27571909, -0.00107294,  0.00029381]

#+begin_src python :var p=p-list :var q=q-list :results output listings :exports results
import sys
sys.path.append('./')
import numpy as np
import simple
#parr = np.array([0.2757333815097, -0.0010721227154135, 0.0002933239156845])
#qarr = np.array([0.27571909, -0.00107294,  0.00029381])
parr = np.array(p)
qarr = np.array(q)
print(simple.doit(parr,qarr))
#+end_src

#+RESULTS:

The doit function just calls numpy.dot()

1 Answer 1

1

Try this:

#+NAME: p-list
#+begin_src python :results value
return [0.2757333815097, -0.0010721227154135, 0.0002933239156845]
#+end_src

#+NAME: q-list
#+begin_src python :results value
return [0.27571909, -0.00107294,  0.00029381]
#+end_src

#+begin_src python :var p=p-list() :var q=q-list() :results output listings :exports results
import sys
sys.path.append('./')
import numpy as np

#parr = np.array([0.2757333815097, -0.0010721227154135, 0.0002933239156845])
#qarr = np.array([0.27571909, -0.00107294,  0.00029381])
parr = np.array(p)
qarr = np.array(q)
print(np.dot(parr,qarr))
#+end_src

#+RESULTS:
: 0.07602619353732326

The variables p and q in the last source block are assigned the values obtained by evaluating the first two code blocks - note the parens.

What you are doing is passing strings to the last block. Consider this:

#+name: foo
[3, 4, 5]

#+begin_src python :var x=foo :results output
print(x)
print(type(x))
#+end_src

#+RESULTS:
: [3, 4, 5]
: 
: <class 'str'>                        <==== It's a string!

EDIT: To reply to the OP's question in a comment: Yes, it is possible to use the simpler string representation; you just need to convert the string to a list inside the Python program.

A way to do that is to use the Python eval function in the program to convert the string to a Python list. Here's a modification of the last example to illustrate:

#+name: foo
[3, 4, 5]

#+begin_src python :var x=foo :results output
print(x)
print(type(x))
x = eval(x)
print(x)
print(type(x))
#+end_src


#+RESULTS:
: [3, 4, 5]
: 
: <class 'str'>
: [3, 4, 5]
: <class 'list'>

Afthe the eval, x is now a list, so you can convert it to a numpy array. Applying this idea to the original program gives:

#+NAME: p-list-as-string
[0.2757333815097, -0.0010721227154135, 0.0002933239156845]

#+NAME: q-list-as-string
[0.27571909, -0.00107294,  0.00029381]

#+begin_src python :var p=p-list-as-string :var q=q-list-as-string :results output listings :exports results
  import sys
  sys.path.append('./')
  import numpy as np

  parr = np.array(eval(p))
  qarr = np.array(eval(q))
  print(np.dot(parr,qarr))
#+end_src

#+RESULTS:
: 0.07602619353732326
5
  • Your answer works. Thanks. Is there a way I can make the vectors appear in the document as normal text vectors. Putting them inside python source blocks isn't how I expect the document to look. Can this work with LaTeX verbatim also? If not, I will still use your solution. Commented Jul 20, 2023 at 2:06
  • See edit for the first part. I don't know what you mean by 'Can this work with LaTeX verbatim also?` but it sounds like a different question, so you should probably create a new question for it. Commented Jul 20, 2023 at 10:50
  • Did you see the updated version? Does that work for you? Commented Jul 23, 2023 at 1:02
  • I will go back and try 'eval'. Commented Jul 24, 2023 at 2:45
  • Did you try it? Did it work? Commented Jul 26, 2023 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.