Any way to program custom syntax to python prompt? >> I want to editmatrices.

M

mosi

Python matrices are usually defined with numpy scipy array or similar.
e.g.
matrix1 = [[1, 2], [3, 4], [5, 6]]
I would like to have easier way of defining matrices,
for example:
matrix = [1, 2; 3, 4; 5, 6]
matrix =
[ 1, 2;
3, 4;
5, 6;]

Any ideas how could this be done? The ";" sign is reserved, the "[ ]"
is used for lists.

Also, how to program custom operations for this new "class?"
matrix ???

For example:[ 3, 4;
5, 6;
7, 8;]

Possibly with operator overloading?
I appreciate all your comments, directions, pointers.
mosi
 
S

sturlamolden

Python matrices are usually defined with numpy scipy array or similar.
e.g.>>> matrix1 = [[1, 2], [3, 4], [5, 6]]

That is a list of Python lists, not a NumPy array or matrix.

For example:>>> matrix + 2

[ 3, 4;
5, 6;
7, 8;]

Possibly with operator overloading?

Go and get NumPy: www.scipy.org

Then buy Travis Oliphant's book.
 
S

Stargaming

Python matrices are usually defined with numpy scipy array or similar.
e.g.
matrix1 = [[1, 2], [3, 4], [5, 6]] I would like to have easier way of defining matrices, for example:
matrix = [1, 2; 3, 4; 5, 6]
matrix =
[ 1, 2;
3, 4;
5, 6;]

Any ideas how could this be done? The ";" sign is reserved, the "[ ]" is
used for lists.

You could have some class `Matrix` with a constructor taking a string, as
in ``Matrix("[1, 2; 3, 4; 5, 6]")``. A very naive parser could just strip
trailing/leading brackets and all spaces, split on ';', split again on
',', done.
Also, how to program custom operations for this new "class?" matrix ???

For example:[ 3, 4;
5, 6;
7, 8;]

Possibly with operator overloading?

Yes, by simply overloading __add__ et al. See http://docs.python.org/ref/
specialnames.html for details (certainly, "Emulating numeric types"
should be interesting to you).
I appreciate all your comments, directions, pointers. mosi

Cheers,
 
R

Ramsey Nasser

Python matrices are usually defined with numpy scipy array or similar.
e.g.
matrix1 = [[1, 2], [3, 4], [5, 6]]
I would like to have easier way of defining matrices, for example:
matrix = [1, 2; 3, 4; 5, 6]
[ 1, 2;
3, 4;
5, 6;]

Any ideas how could this be done? The ";" sign is reserved, the "[ ]" is
used for lists.

I think mosi was looking to dynamically modify the syntax of Python
itself, as opposed to working around the existing syntax. This
includes things like messing with reserved symbols and keywords.

The interpreter is compiled with these definitions built in, so to
change them would conceivably require a recompile. If this is the
case, then mosi may be out of luck. Then again, people have done some
interesting dynamic modifications to the interpreter during runtime
without recompiling. Psyco is the best example of this that comes to
mind. Maybe someone more versed in Psyco-esque interpreter voodoo
would be able to help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top