# Easy way to sum over all values in a group?

**URL:** https://brian.discourse.group/t/easy-way-to-sum-over-all-values-in-a-group/1502
**Category:** Support
**Created:** [28 October 2025 12:18 UTC](https://brian.discourse.group/t/easy-way-to-sum-over-all-values-in-a-group/1502 "2025-10-28T12:18:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kjohnsen](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/kjohnsen/32/200_2.png) [@kjohnsen](https://brian.discourse.group/u/kjohnsen)
#### Post date: [28 October 2025 12:18 UTC](https://brian.discourse.group/t/easy-way-to-sum-over-all-values-in-a-group/1502/1 "2025-10-28T12:18:07Z")

</div>

# 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)

---

<div class="post-metadata">

### Author: ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)
#### Post date: [29 October 2025 10:48 UTC](https://brian.discourse.group/t/easy-way-to-sum-over-all-values-in-a-group/1502/2 "2025-10-29T10:48:49Z")

</div>

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](https://github.com/brian-team/brian2/issues/180) and in particular [Code generation syntax for reductions? · Issue #180 · brian-team/brian2 · GitHub](https://github.com/brian-team/brian2/issues/180#issuecomment-1346922424). 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.

> [@kjohnsen](#):
>
> 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?

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

```python
@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.
