Forum

Name the node


#1

I popped into the IRC channel yesterday to ask if there was a node for something I needed but couldn’t stick around to see the answers so now I’m postin here instead to ask if there already is a node for this purpose or if there’s a need for it and in that case what it should be called.

My input is a spread of consed sub-spreads all having the same number of slices. The output spread should have the number of slices as one sub spread where each slice is the sum of the n:th slice of every subspread of the input

For me it was easier to write a quick plugin than trying to figure out how to patch it in a tidy way. Below is the plugin code:

namespace VVVV.Nodes
{
	#region PluginInfo
	[PluginInfo(Name = "SumNthSlices", Category = "Value", Help = "Basic template with one value in/out", Tags = "")](PluginInfo(Name = "SumNthSlices", Category = "Value", Help = "Basic template with one value in/out", Tags = ""))
	#endregion PluginInfo
	public class ValueSumNthSlicesNode : IPluginEvaluate
	{
		#region fields & pins
		[Input("Input", DefaultValue = 1.0)](Input("Input", DefaultValue = 1.0))
		public IDiffSpread<double> FInput;
		
		[Input("Elements", DefaultValue = 1.0)](Input("Elements", DefaultValue = 1.0))
		public IDiffSpread<int> FElements;
		
		[Output("Output")](Output("Output"))
		public ISpread<double> FOutput;

		[Import()](Import())
		public ILogger FLogger;
		#endregion fields & pins
		
		double[]() result = null;
		int elements = 1;

		//called when data for any output pin is requested
		public void Evaluate(int SpreadMax)
		{

			if( result == null || FElements.IsChanged ) {
				elements = FElements[0](0);
				if( elements <= 0 ) elements = 1;
				result = new double[ elements ]( elements );
			}
			
			if( FInput.IsChanged || FElements.IsChanged ) {
				Array.Clear( result, 0, result.Length);
				for (int i = 0; i < SpreadMax; i++) {
					int index = i % elements;
					result[ index ]( index ) += FInput[i](i);
				}
				FOutput.AssignFrom( result );
			}
		}
	}
}

Is there already any node for this, or a simple way to patch it with a few nodes? The number of slices per sub spread/output and the number of sub spreads must be dynamic.

If not is there a need for it and if so what should it be called?

(I know the code could/should be re-factored a bit since for example now the array will be cleared when it doesn’t need to be after the number of elements changed…)


#2

If you use zip instead of cons, then + do what you want

EDIT: +(value spectral)


#3

… no text …


#4

Or just a regular + where you add the necessary number of inputs and skip the cons/zip stage


#5

But the necessary number of inputs should be able to change without editing the patch. So I don’t see how it would work with your suggestions.


#6

Then use the spectral method


#7

Input is n x m spread.
Where n = number of slices/elements in one sub spread
and m = number of sub spreads
Both n and m should be dynamic, ie the patch should be able to work when the spread grows/shrinks either because of n or m changing. That means no nodes where you have to set the number of input pins (zip, +, … )

Example a)
Number of sub spread / output spread slices = 3
2 “sub spreads”
input: [1.0, 2.0, 3.0, 1.0, 2.0, 3.0](1.0, 2.0, 3.0, 1.0, 2.0, 3.0) with
output a: [ 2.0, 4.0, 6.0 ]( 2.0, 4.0, 6.0 )

Example b)
Number of sub spread / output spread slices = 3
2 “sub spreads”
Input : [1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0](1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0) with
Output : [ 3.0, 6.0, 9.0 ]( 3.0, 6.0, 9.0 )

Example c)
Number of sub spread / output spread slices = 4
2 “sub spreads”
Input : [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0](1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0) with
Output : [ 2.0, 4.0, 6.0, 8.0 ]( 2.0, 4.0, 6.0, 8.0 )

note: I choose easy/sequential numbers for the examples but in practise they could be anything.


#8

you could do like this

AddingSlicesInSpreads.v4p (8.9 kB)


#9

Ok, that seems to work. Thanks!

I edited the patch to make it easier to validate that the result was correct, adding random data didn’t really make it obvious if it was functioning correctly or not.

AddingSlicesInSpreadsEasierToValidateData.v4p (7.8 kB)


#10

you can do it more simple by using the slice count as Vector Size

AddingSlicesInSpreads-2.v4p (10.2 kB)