Multiplying a matrix with a vector

Hello Nengo Community,

I would like to multiply a 1D vector with a Matrix. As a starting point, I am looking at the examples Squaring the input.
What I want to have is a kind of network where when a vector is fed to the network it gets multiplied by a constant matrix. Let say I have a vector y and a matrix A. What I want the network to output = A x y. The network only takes a vector y as an input. Matrix A is a complex one and the output is not complex it is absolute of (A x y).

So what I am thinking is to follow the squaring input example where I am replacing the input square function by defining a function where the input y get’s multiplied by Matrix A.
Is this a correct way to do it?

One more question related to the squaring example. Once the network learns the behaviour, how can we do the inference then? Just like we do in nengo_DL with predict function.

Thank you in advance for your answer.

Hi @Choozi,

There are two ways of multiplying a vector with a matrix in Nengo, and the different methods depend on how the matrix is being populated. Multiplying by a constant matrix is a pretty straightforward implementation, without actually needing any neurons to implement. In this case, you can perform the matrix multiplication by using the transform parameter on a nengo.Connection.

Here’s a simple example of multiplying a vector by the matrix:

[[0, 1, 0, 0]
 [0, 0, 1, 0]
 [0, 0, 0, 1]
 [1, 0, 0, 0]]

And here’s the Nengo code:

import nengo
import numpy as np

with nengo.Network() as model:
    inp = nengo.Node([1, 2, 3, 4])
    out = nengo.Node(size_in=4)

    matrix = np.matrix([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]])

    nengo.Connection(inp, out, transform=matrix, synapse=None)

    p_out = nengo.Probe(out)

with nengo.Simulator(model) as sim:
    sim.run(0.01)

print(sim.data[p_out][-1])

If you run the code above, you should get the output:

[2. 3. 4. 1.]

which is the correct result if you multiply the vector [1, 2, 3, 4] with the matrix, demonstrating that you can use the transform parameter of the nengo.Connection object to do the matrix multiplication for you. But, as noted before, for this method to work, you cannot change the matrix once the network is created.

I should note, however, that the method above doesn’t make any assumptions about the complex / real nature of the matrix elements. Rather, it just assumes that all of the matrix elements are just numbers, and it is up to the Nengo user to interpret the result however they want to. If you want to do matrix multiplication with complex numbers (or vectors containing complex values), then I’d suggest splitting the vector and matrices in their respective real and complex components, and doing each matrix multiplication separately. I.e., you’ll have 4 connections, one to do each of the following multiplications:

  • real x real
  • real x complex
  • complex x real
  • complex x complex

You’ll need to sign the inputs appropriately to make sure that the math works out though (e.g., complex x complex needs a -1 factor).

Side note: we make use of both the transform method to do matrix multiplication, and dealing with real & complex numbers in our implementation of the Circular Convolution operator. This operator is available as a built-in network in Nengo.