[Tutor] 2x2 matrix

ThreeBlindQuarks threesomequarks at proton.me
Wed Dec 27 00:24:53 EST 2023


I decided to try another tack but want to make clear we have to know if the OP wants to make a list of lists or make some form of mathematical object that acts like a matrix so you can do things like multiply with it.

WARNING: This solution depends on things the OP may not yet be using such as comprehensions or use of the range() function and of course numpy. And it is way more general than requested but that can be a good thing.

So here is the plan. Make it in steps. The following code is not a one-liner, albeit it could be.

# if you want a real matrix at the end, import numpy.

import numpy as np

# Number of rows/columns in multiplication table
# reset this to 2 or whatever size you want
n = 5

# generate a list containing 1:n
sides = list(range(1,n+1))

# Make a flat list with all multiplications one row at a time.
flat = [i*j  for i in sides for j in sides]

# Now take every n items in list (a row) and make a list of such lists.
nested = [flat[i : i+n] for i in range(0, len(flat), n)]

# If you want a real matrix, use numpy.
may_tricks = np.array(nested)

# END CODE

So I chose n=5 to make a point that this will work for anything that is 1 or above.

Here is the output of each step if n==5:

>>> n
5
>>> sides
[1, 2, 3, 4, 5]
>>> flat
[1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20, 5, 10, 15, 20, 25]
>>> nested
[[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [5, 10, 15, 20, 25]]
>>> may_tricks
array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])

For n=2 it is simpler:

>>> n
2
>>> sides
[1, 2]
>>> flat
[1, 2, 2, 4]
>>> nested
[[1, 2], [2, 4]]
>>> may_tricks
array([[1, 2],
       [2, 4]])

Note you can add, multiply and so on using the matrix or array format:

>>> may_tricks + may_tricks
array([[2, 4],
       [4, 8]])
>>> may_tricks * may_tricks
array([[ 1,  4],
       [ 4, 16]])

And, if this is something you want to do regularly (no idea why) then a function would make sense like this:

def times_table(n):
  flat = [i*j  for i in list(range(1,n+1)) for j in list(range(1,n+1))]
  return(np.array([flat[i : i+n] for i in range(0, len(flat), n)]))

Below I show it for n being invoked as 2 or 12:

>>> times_table(2)
array([[1, 2],
       [2, 4]])
>>> times_table(12)
array([[  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12],
       [  2,   4,   6,   8,  10,  12,  14,  16,  18,  20,  22,  24],
       [  3,   6,   9,  12,  15,  18,  21,  24,  27,  30,  33,  36],
       [  4,   8,  12,  16,  20,  24,  28,  32,  36,  40,  44,  48],
       [  5,  10,  15,  20,  25,  30,  35,  40,  45,  50,  55,  60],
       [  6,  12,  18,  24,  30,  36,  42,  48,  54,  60,  66,  72],
       [  7,  14,  21,  28,  35,  42,  49,  56,  63,  70,  77,  84],
       [  8,  16,  24,  32,  40,  48,  56,  64,  72,  80,  88,  96],
       [  9,  18,  27,  36,  45,  54,  63,  72,  81,  90,  99, 108],
       [ 10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120],
       [ 11,  22,  33,  44,  55,  66,  77,  88,  99, 110, 121, 132],
       [ 12,  24,  36,  48,  60,  72,  84,  96, 108, 120, 132, 144]])

DISCLAIMER, some other languages make something like this easier and perhaps some python way is along the same lines. For example, in R a matrix is really a vector with added info and rules that provide dimensions so you can do something like ask for a matrix and give it the linear vector (or list) from the above and tell it you want it to be 12 by 12 or 9 by 16 and it does it for you.









Sent with Proton Mail secure email.

On Tuesday, December 26th, 2023 at 10:10 PM, ThreeBlindQuarks <threesomequarks at proton.me> wrote:


> Alan,
> 
> I thought an earlier questions asked not about a "matrix" but a two by two list of lists.
> 
> The answers can be substantially similar, albeit you may have to use a module like numpy that supports a matrix.
> 
> The question keeps showing up in several places and, as you note, the answers do not appear to be evaluated so maybe the right answer is to not.
> 
> Or, throw a comprehension at them instead of an explicit loop.
> 
> But this keeps smelling like homework and not part of a larger purpose.
> 
> 
> 
> 
> Sent with Proton Mail secure email.
> 
> 
> On Tuesday, December 26th, 2023 at 8:36 PM, Alan Gauld via Tutor tutor at python.org wrote:
> 
> 
> 
> > On 26/12/2023 20:27, Ethan Rosenberg wrote:
> > 
> > > I want to put i*j in the cells of a 2x2 matrix and have had no luck.
> > > Help please.
> > 
> > You've asked this a few times now and been given
> > several solutions.
> > 
> > What are you trying currently?
> > What error messages are you getting?
> > If no errors what output are you getting?
> > 
> > I could just post a one line solution, but you'd
> > learn nothing from that. We are here to help you
> > learn not to provide you with solutions.
> > 
> > Tell us what is puzzling you or you find difficult
> > and we are happy to help.
> > 
> > --
> > 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 at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list