Note
The octavemagic extension has been moved to oct2py as oct2py.ipython.
Magics for interacting with Octave via oct2py.
Note
The oct2py module needs to be installed separately and can be obtained using easy_install or pip.
You will also need a working copy of GNU Octave.
To enable the magics below, execute %load_ext octavemagic.
%octave
%octave [-i INPUT] [-o OUTPUT] [-s SIZE] [-f FORMAT] [code [code ...]]
Execute code in Octave, and pull some of the results back into the Python namespace:
In [9]: %octave X = [1 2; 3 4]; mean(X)
Out[9]: array([[ 2., 3.]])
As a cell, this will run a block of Octave code, without returning any value:
In [10]: %%octave
....: p = [-2, -1, 0, 1, 2]
....: polyout(p, 'x')
-2*x^4 - 1*x^3 + 0*x^2 + 1*x^1 + 2
In the notebook, plots are published as the output of the cell, e.g.:
%octave plot([1 2 3], [4 5 6])
will create a line plot.
Objects can be passed back and forth between Octave and IPython via the -i and -o flags in line:
In [14]: Z = np.array([1, 4, 5, 10])
In [15]: %octave -i Z mean(Z)
Out[15]: array([ 5.])
In [16]: %octave -o W W = Z * mean(Z)
Out[16]: array([ 5., 20., 25., 50.])
In [17]: W
Out[17]: array([ 5., 20., 25., 50.])
The size and format of output plots can be specified:
In [18]: %%octave -s 600,800 -f svg
...: plot([1, 2, 3]);
-i INPUT, --input INPUT | |
Names of input variables to be pushed to Octave. Multiple names can be passed, separated by commas with no whitespace. | |
-o OUTPUT, --output OUTPUT | |
Names of variables to be pulled from Octave after executing cell body. Multiple names can be passed, separated by commas with no whitespace. | |
-s SIZE, --size SIZE | |
Pixel size of plots, “width,height”. Default is “-s 400,250”. | |
-f FORMAT, --format FORMAT | |
Plot format (png, svg or jpg). |
%octave_push
Line-level magic that pushes a variable to Octave.
line should be made up of whitespace separated variable names in the IPython namespace:
In [7]: import numpy as np
In [8]: X = np.arange(5)
In [9]: X.mean()
Out[9]: 2.0
In [10]: %octave_push X
In [11]: %octave mean(X)
Out[11]: 2.0
%octave_pull
Line-level magic that pulls a variable from Octave.
In [18]: _ = %octave x = [1 2; 3 4]; y = 'hello'
In [19]: %octave_pull x y
In [20]: x
Out[20]:
array([[ 1., 2.],
[ 3., 4.]])
In [21]: y
Out[21]: 'hello'