Easy way to sum over all values in a group?

Description of problem

I have a complex weight tuning procedure that does linear algebra on the weight matrix, which is a pain to implement with pure Brian since I need to introduce a new set of synapses for every matrix multiplication. For example, I need synapses onto a neuron group with a summed variable to perform matrix multiplication, and then I want to sum over that vector, so I need another group of synapses from ng[:] to ng[0] with a shared, summed variable

So this is more out of curiosity, and I think the answer is “no” from looking at the docs, but is there a way to easily implement vector operations for non-numpy targets? For example, the sum over all the weights in a synapse group? How complicated would it be to add support for this?

What you have already tried

Adding a sum function to the cython target (which then fails because it receives a float instead of an iterable)

Hi @kjohnsen. I agree that this is a weakness of the current Brian syntax, which is very oriented towards per-neuron and sparse synaptic computation – matrix multiplication and similar things are cumbersome. For the summing over neurons, I’ve argued previously that the most logical syntax would be to extend (summed) to NeuronGroup, see Code generation syntax for reductions? · Issue #180 · brian-team/brian2 · GitHub and in particular Code generation syntax for reductions? · Issue #180 · brian-team/brian2 · GitHub. This could be trivially extended to a sum over synapses as well.
No one has been working on this, but it shouldn’t be that difficult to implement.

If you are using a runtime target, a network_operation would be a trivial solution, i.e. using something like

@network_operation
def sum_x_neurons():
    ng.x_sum_ = np.sum(ng.x_[:])

The user-defined function syntax is only meant for scalar functions, unfortunately.