Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:21:50+00:00 2026-06-10T01:21:50+00:00

The science/engineering application I’m working on has lots of linear algebra matrix multiplications, therefore

  • 0

The science/engineering application I’m working on has lots of linear algebra matrix multiplications, therefore I use Numpy matrices. However, there are many functions in python that interchangeably accept matrix or array types. Nice, no? Well, not really. Let me demonstrate the problem with an example:

from scipy.linalg import expm
from numpy import matrix

# Setup input variable as matrix
A = matrix([[ 0, -1.0,  0,  0],
            [ 0,  0,  0,  1.0],
            [ 0,  0,  0,  0],
            [ 0,  0,  1.0,  0]])

# Do some computation with that input
B = expm(A)

b1 = B[0:2, 2:4]
b2 = B[2:4, 2:4].T

# Compute and Print the desired output
print "The innocent but wrong answer:"
print b2 * b1

print "The answer I should get:"
print matrix(b2) * matrix(b1)

When run you get:

The innocent but wrong answer:
[[-0.16666667 -0.5       ]
 [ 0.          1.        ]]
The answer I should get, since I expected everything to still be matrices:
[[ 0.33333333  0.5       ]
 [ 0.5         1.        ]]

Any tips or advice on how to avoid this sort of a mix up? Its really messy to keep wrapping variables in matrix() calls to ensure they still are matrices. It seems there is no standard in this regard, and so it can lead to bugs that are hard to detect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:21:52+00:00Added an answer on June 10, 2026 at 1:21 am

    I tend to use array instead of matrix in numpy for a few reasons:

    1. matrix is strictly 2D whereas you can have a numpy array of any dimension.
    2. Aside from a few differences, array and matrix operations are pretty much interchangeable for a Matlab user.
    3. If you use array consistently, then you would use numpy.dot() (or in Python 3.5 the new @ binary operator) for matrix multiplication. This will prevent the problem of not sure what * actually does in your code. And when you encounter a multiplication error, you can find the problem easier since you are certain of what kind of multiplication you are trying to perform.

    So I would suggest you try to stick to numpy.array, but also keep in mind the differences between array and matrix.

    Lastly, I found it a joy to work with numpy/scipy on bpython. The auto-prompt helps you to learn the properties of the function you are trying to use at a much faster pace than having to consult the numpy/scipy doc constantly.

    Edit:
    The difference between array vs matrix is perhaps best answered here: ‘array’ or ‘matrix’? Which should I use?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a computer science student currently doing an internship. My boss has asked
I am planning to make a Plagiarism Detector as my Computer Science Engineering final
This question may seem rather basic, but coming from an engineering (non computer-science) background,
I am a computer science student and my project partner and I are working
I'm a Computer Science student and I'm working on an Android app. for my
I'm a computer science student at a large university. The department has had it's
I am doing 2nd year computer science and we have a software engineering group
I work in an engineering lab, not a computer science lab. As such, our
I'm a student doing final year of my Computer Science and Engineering. For my
I'm a computer science student at a university working on an independent study. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.