Ask Your Question

Given a value and conditions, find a matrix with that value as its determinant

asked 2019-02-21 06:01:41 -0500

LukeC93 gravatar image

I'm not sure if the title is named too obscurely, so I'll give an example of what I'm talking.

Say we have an integer matrix: M= 0 a b 0 (sorry for weird formatting; cannot seem to write matrices properly on here - any help with that as well? :P )

I want to find all values $(a,b) \in (-2,-1,0,1,2)$ such that $det(M)=2$.

I've tried the following but I'm not sure how to finish it off / if this is the right way to go:

    import itertools
    for (a,b) in itertools.product([-2,-1,0,1,2], repeat=2):
          M = Matrix(ZZ, [[0,a],[b,0]])
    if M.determinant()==2:
         min_params = [(a,b)]
    print 'Sets of parameters with this determinant:', min_params

As I say, I feel like the start would be a good way to go, but I'm stuck when reaching elif, and I'm not sure if this is the right way to tackle this anyway!

Any help would be great!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-02-21 06:37:42 -0500

Emmanuel Charpentier gravatar image

This works for me :

import itertools
min_params=[] ## Do,'t forget to intitialize !
for (a,b) in itertools.product([u for u in (-2..2)], repeat=2):
    M = Matrix(ZZ, [[0,a],[b,0]])
    if M.determinant()==2: ## Mind the indentation : this is part of the loop...
         min_params.append((a,b)) ## No need for an else clause
print 'Sets of parameters with this determinant:', min_params

Sets of parameters with this determinant: [(-2, 1), (-1, 2), (1, -2), (2, -1)]

You seem to struggle with the basics of the (admittedly peculiar) Python syntax, where (among others) whitespace is syntaxic...

I'd suggest to peruse a good Python tutorial ; keep in mind that Sage is currently Python2-based, but should soon (?) be Python3-based (and yes, there are a lot of devils in the seemingly small details...).


edit flag offensive delete link more


Amazing, thank you!

I'm very much a novice in Sage and Python, and have yet to dedicate time to trying to learn it properly, instead opting to just piece things together as and when. Think it might be time for me to go to basics..

LukeC93 gravatar imageLukeC93 ( 2019-02-21 07:16:28 -0500 )edit

You can't (profitably) don your pants, then add your underwear as an afterthought...

May I recommend Computational Mathematics with SageMath as a study guide for Sage ? ISTR that it points you to satisfying Python tutorials when/where needed...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-02-21 10:16:06 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-02-21 06:01:41 -0500

Seen: 33 times

Last updated: Feb 21