Re: [Tutor] Matrix Multiplication user entries

2018-03-23 Thread Mats Wichmann
On 03/23/2018 12:02 AM, Noor Alghanem wrote:
> Hello,
> 
> I am trying to write a program that is basically multiplying two matrices
> one of size 1x8 and the other one of size 8x4. The different thing that I
> am trying to figure out is that I want the program to ask the user to enter
> the values for the 1x8 matrix only which will be used in the multiplication
> process. How do I do that, please provide an example as I wasn't able to
> find one online that works. Also, if you can please include an example of
> matrix multiplication that is similar in idea to what I am trying to write.

For asking questions here, we really like if you show something you've
tried, so we can point out where it can be improved.

Some hints: for the second part, assuming I get what you want, you have
to change the shape of one matrix so you can multiply. So if your one
matrix will be a and the other b, something like:

import numpy

a = numpy.array(... initialization of 8x4 matrix ...)
b = numpy.array(... initialization of 1x8 matrix ...)
b.resize(a.shape)   #
print a * b

# or, depending on what you want (check the documentation - one form
# zero-extends, the other duplicates):
b = numpy.resize(b, a.shape)
print a * b


print things out along the way if you're not sure what they're doing


does this help?  (it's trickier if you want to avoid numpy! - I can't
tell if this is homework that has some specific requirements on what you
can and cannot use)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix Multiplication user entries

2018-03-23 Thread Noor Alghanem
Hello,

I am trying to write a program that is basically multiplying two matrices
one of size 1x8 and the other one of size 8x4. The different thing that I
am trying to figure out is that I want the program to ask the user to enter
the values for the 1x8 matrix only which will be used in the multiplication
process. How do I do that, please provide an example as I wasn't able to
find one online that works. Also, if you can please include an example of
matrix multiplication that is similar in idea to what I am trying to write.

Thank you
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix help

2018-03-22 Thread Alan Gauld via Tutor
On 22/03/18 11:35, Connie Callaghan wrote:
> Hi, 
> I was just looking help for a matrix that I am building, it needs to look 
> like this 
> 1, 0, 0, ...,0
> A,b,c,0,...,0
> 0,a,b,c,...,0
> 0,0,a,b,c,..0
> 0,0,0,a,b,c,...,0
> 0,0,0,0...0, 1 

What exactly are the a,b,c values?
Are they variables or literal characters?
Or something else?

Also, how many cells do the ... represent?
Do you know in advance? or are they dynamically calculated?
If the latter then based on what?

The first and last rows are completely different - no a,b,c
values - how are they determined?
ie How do you know you have reached the last row?

We need a much more rigorous specification.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix help

2018-03-22 Thread Peter Otten
Connie Callaghan wrote:

> Hi,
> I was just looking help for a matrix that I am building, it needs to look
> like this 1, 0, 0, ...,0
> A,b,c,0,...,0
> 0,a,b,c,...,0
> 0,0,a,b,c,..0
> 0,0,0,a,b,c,...,0
> 0,0,0,0...0, 1
> 
> It has n rows and columns and the first and last line has to have 1s at
> the corners as shown, and a,b,c going diagonal and 0’s everywhere else, I
> am really struggling and it would be a great help to even be shown how to
> begin,

Assuming you are not using numpy the easiest way is to start with a list of 
lists containing only zeros:

>>> N = 5
>>> my_matrix = [[0] * N for row in range(N)]
>>> my_matrix
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 
0, 0, 0]]

This becomes a bit more readable with pprint:

>>> from pprint import pprint
>>> pprint(my_matrix)
[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

You can then modify the matrix:

>>> my_matrix[0][0] = my_matrix[-1][-1] = 1
>>> pprint(my_matrix)
[[1, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1]]

For the other values use a for loop, like

>>> for i in range(1, N):
... my_matrix[i][i-1] = 42
... 
>>> pprint(my_matrix)
[[1, 0, 0, 0, 0],
 [42, 0, 0, 0, 0],
 [0, 42, 0, 0, 0],
 [0, 0, 42, 0, 0],
 [0, 0, 0, 42, 1]]

Oops, one more 42 than b-s in your example. Looks like you need to adjust 
the range() arguments.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix help

2018-03-22 Thread Connie Callaghan
Hi, 
I was just looking help for a matrix that I am building, it needs to look like 
this 
1, 0, 0, ...,0
A,b,c,0,...,0
0,a,b,c,...,0
0,0,a,b,c,..0
0,0,0,a,b,c,...,0
0,0,0,0...0, 1 

It has n rows and columns and the first and last line has to have 1s at the 
corners as shown, and a,b,c going diagonal and 0’s everywhere else, I am really 
struggling and it would be a great help to even be shown how to begin, 

Thanks 
Connie 




---
This email has been checked for viruses by AVG.
http://www.avg.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix bug

2015-04-05 Thread Steven D'Aprano
On Sun, Apr 05, 2015 at 11:12:32AM -0300, Narci Edson Venturini wrote:
> The next code has an unexpected result:
> 
> >>>a=3*[3*[0]]
> >>>a
> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
> >>>a[0][0]=1
> >>>a
> [[1, 0, 0], [1, 0, 0], [1, 0, 0]]

It isn't obvious, and it is *very* common for people to run into this 
and be confused, but that is actually working by design. The * operator 
for lists does not copy the list items, it makes multiple references to 
it. Let's have a look at an example. We start with an arbitrary object:

py> class X: pass
...
py> x = X()
py> print(x)
<__main__.X object at 0xb7a9d92c>

Printing the object x shows us the memory location of x: 0xb7a9d92c. Now 
let us put it in a list, and replicate it:

py> mylist = [x]*3
py> print(mylist)
[<__main__.X object at 0xb7a9d92c>, <__main__.X object at 0xb7a9d92c>, 
<__main__.X object at 0xb7a9d92c>]


Do you see how *all three* list items have the same memory location? 
Rather than get three different X objects, we have the same object, 
repeated three times. So these two lines are essentially identical:

mylist = [x]*3
mylist = [x, x, x]


Now, in practice, sometimes that makes a difference, and sometimes it 
doesn't. If you use an int or a str, it makes no difference:

py> mylist = [1]*5
py> mylist
[1, 1, 1, 1, 1]

Let's look at their ID numbers and see that they are identical:

py> for item in mylist:
... print(id(item))
...
136560640
136560640
136560640
136560640
136560640

So it is the same int object repeated five times, not five different 
objects. But that doesn't matter, since there is no way to change the 
value of the object: ints are immutable, and 1 is always 1. You can only 
*replace* the object with a new object:

py> mylist[0] = 2
py> print(mylist)
[2, 1, 1, 1, 1]


Now let's do it again with a list of lists:


py> mylist = [[]]*5
py> mylist
[[], [], [], [], []]
py> for item in mylist:
... print(id(item))
...
3081330988
3081330988
3081330988
3081330988
3081330988


So you can see, we now have the same list repeated five times, not five 
different lists. If we *replace* one of the items, using = assignment, 
everything behaves as expected:

py> mylist[0] = [1,2,3]  # Replace the first item.
py> print(mylist)
[[1, 2, 3], [], [], [], []]


But if we modify one of the items, you may be surprised:

py> mylist[1].append(999)  # Change the second item in place.
py> print(mylist)
[[1, 2, 3], [999], [999], [999], [999]]



The solution to this "gotcha" is to avoid list multiplication except for 
immutable objects like ints and strings. So to get a 3 x 3 array of all 
zeroes, I would write:

py> array = [[0]*3 for i in range(3)]
py> print(array)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
py> array[0][1] += 1
py> print(array)
[[0, 1, 0], [0, 0, 0], [0, 0, 0]]


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix bug

2015-04-05 Thread Alan Gauld

On 05/04/15 15:12, Narci Edson Venturini wrote:

The next code has an unexpected result:


a=3*[3*[0]]


Note that this makes three references to
the list of 3 references to 0.
In other words you reference the same list 3 times.
So when you change the first copy you change the
other 2 also.

Put another way:

>>> mylist = [0,0,0]
>>> a = 3 * [mylist]

'a' now contains 3 references to mylist, making
4 references altogether to the same list object.
If I change the list via any of them it will
change in all the places it is referenced:

>>> mylist[0] = 1   # replace a zero with a one
>>> mylist
[1, 0, 0]
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]


When the followind code is ran, them the correct result is obtained:


a=[[0 for i in range(3)] for j in range(3)]


This created 3 new lists. In fact you could simplify it to:

a = [[0,0,0] for i in range(3)]

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix bug

2015-04-05 Thread Emile van Sebille

On 4/5/2015 7:12 AM, Narci Edson Venturini wrote:

The next code has an unexpected result:


a=3*[3*[0]]


a now contains three references to the same object, hence the results 
you show below.


You can create three distinct objects as follows:

>>> a = [ [0,0,0] for i in (0,1,2) ]
>>> a[1][1]=1
>>> a
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
>>>

hth,

Emile




a

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

a[0][0]=1
a

[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

The code assigned to "1" a(0,0), a(1,0) and a(2,0).

It was expected: [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

When the followind code is ran, them the correct result is obtained:


a=[[0 for i in range(3)] for j in range(3)]

a

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

a[0][0]=1
a

  [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

So, what is wrong ?

Best Regars,



Narci
__
Narci Edson Venturini
(19) 99733-8420
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix bug

2015-04-05 Thread Narci Edson Venturini
The next code has an unexpected result:

>>>a=3*[3*[0]]
>>>a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>>a[0][0]=1
>>>a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

The code assigned to "1" a(0,0), a(1,0) and a(2,0).

It was expected: [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

When the followind code is ran, them the correct result is obtained:

>>>a=[[0 for i in range(3)] for j in range(3)]
>>a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>>a[0][0]=1
>>>a
 [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

So, what is wrong ?

Best Regars,



Narci
__
Narci Edson Venturini
(19) 99733-8420
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix Multiplication and its Inverse

2013-07-13 Thread Steven D'Aprano

On 13/07/13 05:05, Jack Little wrote:

Is there a way in python to do matrix multiplication and its inverse? No 
external modules is preferred, but it is ok.



If you have numpy, you should use that.

If you want a pure Python version, here's a quick and dirty matrix multiplier 
that works only for 2x2 matrices.


def is_matrix(obj):
if len(obj) == 2:
return len(obj[0]) == 2 and len(obj[1]) == 2
return False


def matrix_mult(A, B):
"""Return matrix A x B."""
if not (is_matrix(A) and is_matrix(B)):
raise ValueError('not matrices')
[a, b], [c, d] = A
[w, x], [y, z] = B
return [[a*w + b*y, a*x + b*z],
[c*w + d*y, c*x + d*z]]


I leave the inverse as an exercise :-)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matrix-vector multiplication errors

2008-02-02 Thread Eike Welk
On Friday 01 February 2008 23:13, Dinesh B Vadhia wrote:
> I've posted this on the Scipy forum but maybe there are answers on
> Tutor too.  I'm performing a standard Scipy matrix* vector
> multiplication, b=Ax , (but not using the sparse module) with
> different sizes of A as follows:
>
>
> Assuming 8 bytes per float, then:
> 1. matrix A with M=10,000 and N=15,000 is of approximate size:
> 1.2Gb 2. matrix A with M=10,000 and N=5,000 is of approximate size:
> 390Mb 3. matrix A with M=10,000 and N=1,000 is of approximate size:
> 78Mb
>
> The Python/Scipy matrix initialization statements are:
> > A = scipy.asmatrix(scipy.empty((I,J), dtype=int))
> > x = scipy.asmatrix(scipy.empty((J,1), dtype=float))
> > b = scipy.asmatrix(scipy.empty((I,1), dtype=float))
>
> I'm using a Windows XP SP2 PC with 2Gb RAM.
>
> Both matrices 1. and 2. fail with INDeterminate values in b. 
> Matrix 3. works perfectly.  As I have 2Gb of RAM why are matrices
> 1. and 2. failing?
>
> The odd thing is that Python doesn't return any error messages with
> 1. and 2. but we know the results are garbage (literally!)
>
> Cheers!
>
> Dinesh

I suspect that you have some uninitialized elements accidentally left 
in A or x; similarly to what Matthieu said in the Scipy list. You 
could try to initialize all elements to zero, and see if the errors 
still happen. It'll take some additional time but it's not too bad:

In [1]:import numpy

In [2]:M=1

In [3]:N=15000

In [4]:time A = numpy.matrix(numpy.zeros((M,N), dtype=int))
CPU times: user 1.14 s, sys: 3.01 s, total: 4.15 s
Wall time: 44.37

In [5]:time A = numpy.matrix(numpy.empty((M,N), dtype=int))
CPU times: user 0.65 s, sys: 1.12 s, total: 1.76 s
Wall time: 6.11


Regards, 
Eike.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matrix-vector multiplication errors

2008-02-02 Thread Eike Welk
On Friday 01 February 2008 23:13, Dinesh B Vadhia wrote:
> I've posted this on the Scipy forum but maybe there are answers on
> Tutor too.  I'm performing a standard Scipy matrix* vector
> multiplication, b=Ax , (but not using the sparse module) with
> different sizes of A as follows:
>
>
> Assuming 8 bytes per float, then:
> 1. matrix A with M=10,000 and N=15,000 is of approximate size:
> 1.2Gb 2. matrix A with M=10,000 and N=5,000 is of approximate size:
> 390Mb 3. matrix A with M=10,000 and N=1,000 is of approximate size:
> 78Mb
>
> The Python/Scipy matrix initialization statements are:
> > A = scipy.asmatrix(scipy.empty((I,J), dtype=int))
> > x = scipy.asmatrix(scipy.empty((J,1), dtype=float))
> > b = scipy.asmatrix(scipy.empty((I,1), dtype=float))
>
> I'm using a Windows XP SP2 PC with 2Gb RAM.
>
> Both matrices 1. and 2. fail with INDeterminate values in b. 
> Matrix 3. works perfectly.  As I have 2Gb of RAM why are matrices
> 1. and 2. failing?
>
> The odd thing is that Python doesn't return any error messages with
> 1. and 2. but we know the results are garbage (literally!)
>
> Cheers!
>
> Dinesh

I suspect that you have some uninitialized elements accidentally left 
in A or x; similarly to what Matthieu said in the Scipy list. You 
could try to initialize all elements to zero, and see if the errors 
still happen. It'll take some additional time but it's not too bad:

In [1]:import numpy

In [2]:M=1

In [3]:N=15000

In [4]:time A = numpy.matrix(numpy.zeros((M,N), dtype=int))
CPU times: user 1.14 s, sys: 3.01 s, total: 4.15 s
Wall time: 44.37

In [5]:time A = numpy.matrix(numpy.empty((M,N), dtype=int))
CPU times: user 0.65 s, sys: 1.12 s, total: 1.76 s
Wall time: 6.11


Regards, 
Eike.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] matrix-vector multiplication errors

2008-02-01 Thread Dinesh B Vadhia
I've posted this on the Scipy forum but maybe there are answers on Tutor too.  
I'm performing a standard Scipy matrix* vector multiplication, b=Ax , (but not 
using the sparse module) with different sizes of A as follows:  


Assuming 8 bytes per float, then:
1. matrix A with M=10,000 and N=15,000 is of approximate size: 1.2Gb
2. matrix A with M=10,000 and N=5,000 is of approximate size: 390Mb
3. matrix A with M=10,000 and N=1,000 is of approximate size: 78Mb

The Python/Scipy matrix initialization statements are:
> A = scipy.asmatrix(scipy.empty((I,J), dtype=int))
> x = scipy.asmatrix(scipy.empty((J,1), dtype=float))
> b = scipy.asmatrix(scipy.empty((I,1), dtype=float))

I'm using a Windows XP SP2 PC with 2Gb RAM.

Both matrices 1. and 2. fail with INDeterminate values in b.  Matrix 3. works 
perfectly.  As I have 2Gb of RAM why are matrices 1. and 2. failing?

The odd thing is that Python doesn't return any error messages with 1. and 2. 
but we know the results are garbage (literally!)

Cheers!

Dinesh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2005-10-26 Thread Alan Gauld
This doesn't seem to have been answered...

"Shi Mu" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I can not understand the use of "cell in row" for two times in the code:
>
> # convert the matrix to a 1D list
> matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
> items = [cell for row in matrix for cell in row]
> print items

Lets expand the list comprehension:

matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
items = []
for row in matrix:
 for cell in row:
  items.append(cell)
print items

Does that explain whats going on? Its just nesting another for loop.

HTH,


-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix

2005-10-24 Thread Shi Mu
I can not understand the use of "cell in row" for two times in the code:

# convert the matrix to a 1D list
matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
items = [cell for row in matrix for cell in row]
print items
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2005-01-14 Thread Hugo González Monteverde

Brian van den Broek wrote:
2) To get around that, and be more efficient with matricies with many 
empty cells:
.>>> my_matrix_as_dict = {(1,1):4, (1,2):6, (1,3):8,
 (2,1):56, (2,3):12,
 (3,1):3, (3,2):3}

.>>> my_matrix_as_dict[(3,1)]
3
.>>> my_matrix_as_dict[(2,1)]
56
So, you just can use the tuple co-ordinates you've defined in order to 
access cells. Note also that the list way you'd have to represent empty 
cells with a standard null value -- None is the usual choice. But this 
way, you just don't define some tuples as keys, as I didn't define (2,2) 
as a key. Thus:

.>>> my_matrix_as_dict[(2,2)]
Traceback (most recent call last):
  File "", line 1, in -toplevel-
my_matrix_as_dict[(2,2)]
KeyError: (2, 2)
You can make that more graceful with a try/except block:
.>>> try:
my_matrix_as_dict[(2,2)]
except KeyError:
print "That cell is empty"

That cell is empty
.>>>

if you want None to be returned for an empty cell, you can just do:
my_matrix_as_dict.get((2,2), None)
so that  the None object will de returned for all inexistant cells...
Hugo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-20 Thread Bob Gailer

On Sun, 19 Dec 2004, Bugra Cakir wrote:
> I want to create a matrix in Python. For example 3x4 how can i
> create this? thanks
Just for the heck of it would you tell us what you want to do with the 
matrix? Sometimes there are interesting alternate ways to do things that 
might not be obvious to a relatively new Pythoneer.

Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-20 Thread Danny Yoo


On Sun, 19 Dec 2004, Bugra Cakir wrote:

> I want to create a matrix in Python. For example 3x4 how can i
> create this? thanks


Hi Bugra,

Just for reference, here's the relevant FAQ about how to do matrices in
Python:

http://python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list


If you're planning to do a lot of matrix-y stuff, you may want to look
into the 'numarray' third-module:

http://www.stsci.edu/resources/software_hardware/numarray

The package adds powerful matrix operations to Python.


Good luck to you!

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-20 Thread Alan Gauld
> A list of lists is the most logical construction.
> But -- you don't need to assemble the rows separately --
> this works too:
>  >>> my_matrix_as_lists = [ [4, 6, 8, 1],\
> ...   [2, 5, 1, 3],\
> ...   [2, 1, 2, 8] ]

And you don't need the line continuation characters either. 
Because Python can tell that the brackets don't match you 
can just add the newlines directly:

>>> mx = [ [1,2,3,4],
...[5,6,7,8] ]
>>>

Alan g
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-19 Thread Brian van den Broek
Rob Kapteyn said unto the world upon 2004-12-19 22:44:
Hello to the list !
A list of lists is the most logical construction.
But -- you don't need to assemble the rows separately --
this works too:
 >>> my_matrix_as_lists = [ [4, 6, 8, 1],\
...   [2, 5, 1, 3],\
...  [2, 1, 2, 8] ]
 >>> print my_matrix_as_lists[1][1]
5
Rob
On Dec 19, 2004, at 1:31 PM, Brian van den Broek wrote:
Bugra Cakir said unto the world upon 2004-12-19 10:33:
hi,
I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
Hi,
at least two ways using only builtins occur to me:
1) A list of lists:
.>>> row1 = [4, 6, 8, 1]
.>>> row2 = [2, 5, 1, 3]
.>>> row3 = [2, 1, 2, 8]
.>>> my_matrix_as_lists = [row1, row2, row3]
.>>> print my_matrix_as_lists[1][1]
5

HTH,
Brian vdB
Sure, there's no need to assemble the rows separately. But, if you 
collapse it as you did, there is no need for the line continuations, 
either. Since all the lines are enclosed within brackets, if you want to 
span multiple lines, you can just as well do:

.>>> my_matrix_as_lists = [ [4, 6, 8, 1],
[2, 5, 1, 3],
[2, 1, 2, 8] ]
Best to all,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-19 Thread Rob Kapteyn
Juan:
Thanks for the tip, but the correct address seems to be:
http://www.scipy.com/
the .org does not answer.
Now I have to check out the graphing tools they have:-)
Rob
On Dec 19, 2004, at 8:01 PM, Juan Shen wrote:
Try SciPy.
   http://www.scipy.org/
It has mat class to handle matrix and much else.
   Juan
Bugra Cakir wrote:
hi,
I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-19 Thread Rob Kapteyn
Hello to the list !
A list of lists is the most logical construction.
But -- you don't need to assemble the rows separately --
this works too:
>>> my_matrix_as_lists = [ [4, 6, 8, 1],\
...   [2, 5, 1, 3],\
...   [2, 1, 2, 8] ]
>>> print my_matrix_as_lists[1][1]
5
Rob
On Dec 19, 2004, at 1:31 PM, Brian van den Broek wrote:
Bugra Cakir said unto the world upon 2004-12-19 10:33:
hi,
I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
Hi,
at least two ways using only builtins occur to me:
1) A list of lists:
.>>> row1 = [4, 6, 8, 1]
.>>> row2 = [2, 5, 1, 3]
.>>> row3 = [2, 1, 2, 8]
.>>> my_matrix_as_lists = [row1, row2, row3]
.>>> print my_matrix_as_lists[1][1]
5
Note that
>>> row1[1]
6
since indicies start at 0.
2) To get around that, and be more efficient with matricies with many 
empty cells:
.>>> my_matrix_as_dict = {(1,1):4, (1,2):6, (1,3):8,
 (2,1):56, (2,3):12,
 (3,1):3, (3,2):3}

.>>> my_matrix_as_dict[(3,1)]
3
.>>> my_matrix_as_dict[(2,1)]
56
So, you just can use the tuple co-ordinates you've defined in order to 
access cells. Note also that the list way you'd have to represent 
empty cells with a standard null value -- None is the usual choice. 
But this way, you just don't define some tuples as keys, as I didn't 
define (2,2) as a key. Thus:

.>>> my_matrix_as_dict[(2,2)]
Traceback (most recent call last):
  File "", line 1, in -toplevel-
my_matrix_as_dict[(2,2)]
KeyError: (2, 2)
You can make that more graceful with a try/except block:
.>>> try:
my_matrix_as_dict[(2,2)]
except KeyError:
print "That cell is empty"

That cell is empty
.>>>
HTH,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-19 Thread Juan Shen
Try SciPy.
   http://www.scipy.org/
It has mat class to handle matrix and much else.
   Juan
Bugra Cakir wrote:
hi,
I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
 


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Matrix

2004-12-19 Thread Brian van den Broek
Bugra Cakir said unto the world upon 2004-12-19 10:33:
hi,
I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
Hi,
at least two ways using only builtins occur to me:
1) A list of lists:
.>>> row1 = [4, 6, 8, 1]
.>>> row2 = [2, 5, 1, 3]
.>>> row3 = [2, 1, 2, 8]
.>>> my_matrix_as_lists = [row1, row2, row3]
.>>> print my_matrix_as_lists[1][1]
5
Note that
>>> row1[1]
6
since indicies start at 0.
2) To get around that, and be more efficient with matricies with many 
empty cells:
.>>> my_matrix_as_dict = {(1,1):4, (1,2):6, (1,3):8,
 (2,1):56, (2,3):12,
 (3,1):3, (3,2):3}

.>>> my_matrix_as_dict[(3,1)]
3
.>>> my_matrix_as_dict[(2,1)]
56
So, you just can use the tuple co-ordinates you've defined in order to 
access cells. Note also that the list way you'd have to represent empty 
cells with a standard null value -- None is the usual choice. But this 
way, you just don't define some tuples as keys, as I didn't define (2,2) 
as a key. Thus:

.>>> my_matrix_as_dict[(2,2)]
Traceback (most recent call last):
  File "", line 1, in -toplevel-
my_matrix_as_dict[(2,2)]
KeyError: (2, 2)
You can make that more graceful with a try/except block:
.>>> try:
my_matrix_as_dict[(2,2)]
except KeyError:
print "That cell is empty"

That cell is empty
.>>>
HTH,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix

2004-12-19 Thread Bugra Cakir
hi,

I want to create a matrix in Python. For example 3x4 how can i
create this? thanks
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor