Ask Your Question
0

Create single-column matrix (aka. column vector)

asked 2022-12-01 14:42:19 +0100

drumath gravatar image

updated 2022-12-04 11:30:50 +0100

I have a function that creates matrices of dimension $3r\times r$ with variables:

def create_matrix_with_variables(r):
    m=3*r
    variable_names_string=['b_%d_%d' %(i,j) for (i,j) in cartesian_product([range(1,m+1),range(1,r+1)])]
    parent_ring=ZZ[variable_names_string]
    betas=list(parent_ring.variable_names())
    beta=matrix(parent_ring,m,r,betas)
    return beta

create_matrix_with_variables(2)
create_matrix_with_variables(1)

It works for $r>1$ but fails for $r=1$. I would love to post a screenshot but my karma is not enough. How do I do this correctly for $r=1$? I am using Sage 9.7 on mac in jupyter. Thanks.

UPDATE: Bugfix ticket submitted here: https://trac.sagemath.org/ticket/34821

edit retag flag offensive close merge delete

Comments

Looks like a bug. As a workaround, you could create an r x 3r matrix and take its transpose; that seems to work better.

John Palmieri gravatar imageJohn Palmieri ( 2022-12-02 03:58:25 +0100 )edit

Thanks for the idea @John Palmieri, it should work, yes. I wanted to know if there's something I'm missing but folks here also say it's a bug. I'll go with @Emmanuel Charpentier's answer and I'll try to submit a ticket.

drumath gravatar imagedrumath ( 2022-12-04 11:02:32 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-12-02 19:43:55 +0100

Emmanuel Charpentier gravatar image

Works fine on 9.7.beta3 ... provided you use parent_ring.gens() (i. e. the indeterminates, Sage objects wrapping Singular objects) rather than parent_ring.variable_names() (the indeterminates' names as strings) to build beta. After running :

def create_matrix_with_variables(r):
    m=3*r
    variable_names_string=['b_%d_%d' %(i,j) for (i,j) in cartesian_product([range(1,m+1),range(1,r+1)])]
    parent_ring=ZZ[variable_names_string]
    betas=list(parent_ring.gens())
    beta=matrix(parent_ring,m,r,betas)
    return beta

I get :

sage: create_matrix_with_variables(2)
[b_1_1 b_1_2]
[b_2_1 b_2_2]
[b_3_1 b_3_2]
[b_4_1 b_4_2]
[b_5_1 b_5_2]
[b_6_1 b_6_2]
sage: create_matrix_with_variables(1)
[b_1_1]
[b_2_1]
[b_3_1]

BTW, you may simplify your creation as :

def create2(r, base_name="b"):
    parent_ring=PolynomialRing(ZZ, r*3*r,
                               flatten([["%s_%s_%s"%(base_name, u, v)
                                        for v in (1..r)]
                                       for u in (1..3*r)]))
    return matrix(parent_ring, 3*r, r, parent_ring.gens())

If necessary, inject_variables on the base_ring of your matrix...

That said, the behaviour using the strings is a bug. Care to file a ticket ?

HTH,

edit flag offensive delete link more

Comments

Thank you for pointing out .gens() and also for the improved implementation. I have been trying to migrate from Mathematica to SageMath out of curiosity and faith (to support open research), and it's been a bumpy adventure. Your answer helps a lot :)

drumath gravatar imagedrumath ( 2022-12-04 11:00:27 +0100 )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

Stats

Asked: 2022-12-01 14:42:19 +0100

Seen: 179 times

Last updated: Dec 04 '22