Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Sage Beginner's Guide
  • Table Of Contents Toc
Sage Beginner's Guide

Sage Beginner's Guide

By : Craig Finch
3.9 (11)
close
close
Sage Beginner's Guide

Sage Beginner's Guide

3.9 (11)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
close
close
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1
Index

Time for action – creating matrices in NumPy


To illustrate some of the similarities and differences between the linear algebra features of Sage and NumPy, we'll repeat an earlier example in which we computed the singular value decomposition of a matrix:

import numpy as np
print "Two ways of creating a Numpy matrix:"
A = np.matrix('1 1; 1 1; 0 0')    # Matlab syntax
print(A)
A2 = np.matrix([[1,1], [1,1], [0,0]])
print(A2)

print("Singular value decomposition:")
U, s, Vstar = np.linalg.svd(A, full_matrices=False)
print("U:")
print(U)
print("s:")
print(s)
print("Transpose of conjugate of V:")
print(Vstar)

Sigma = np.diag(s)
print("Reconstructed matrix Sigma:")
print(Sigma)
print(np.dot(U, np.dot(Sigma, Vstar)))

The result should look like this:

What just happened?

The example demonstrated two ways of creating a NumPy matrix object. To start with, we used the statement import numpy as np so that we can use np as a shortcut for numpy. This feature is handy for long package names that are used...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Sage Beginner's Guide
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon