Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Linear algebra and polynomials

In this article we briefly introduce some of NumPy's linear algebra and
polynomial functionality.
© CC-BY-NC-SA 4.0 by CSC - IT Center for Science Ltd.

Linear algebra

NumPy includes linear algebra routines that can be quite handy.

For example, NumPy can calculate matrix and vector products efficiently (dot,
vdot), solve eigenproblems (linalg.eig, linalg.eigvals), solve linear
systems (linalg.solve), and do matrix inversion (linalg.inv).

A = numpy.array(((2, 1), (1, 3)))
B = numpy.array(((-2, 4.2), (4.2, 6)))

C =, B)
b = numpy.array((1, 2))

# output:
# [[ 0.2 14.4]
# [ 10.6 22.2]]

# output: [1 2]

# solve C x = b
x = numpy.linalg.solve(C, b)

# output: [ 0.04453441 0.06882591]

Normally, NumPy utilises high performance numerical libraries in linear
algebra operations. This means that the performance of NumPy is actually quite
good and not far e.g. from the performance of a pure-C implementations using
the same libraries.


NumPy has also support for polynomials. One can for example do least square
fitting, find the roots of a polynomial, and evaluate a polynomial.

A polynomial f(x) is defined by an 1D array of coefficients (p) with
length N, such that (f(x) = p[0] x^{N-1} + p[1] x^{N-2} + … + p[N-1]).

# f(x) = x^2 + random noise (between 0,1)
x = numpy.linspace(-4, 4, 7)
f = x**2 + numpy.random.random(x.shape)

p = numpy.polyfit(x, f, 2)

# output: [ 0.96869003 -0.01157275 0.69352514]
# f(x) = p[0] * x^2 + p[1] * x + p[2]
© CC-BY-NC-SA 4.0 by CSC - IT Center for Science Ltd.
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education