This project has moved and is read-only. For the latest updates, please go here.

Example of using foldRows/foldCoumns for matrix

Jun 9, 2014 at 11:53 AM

it would be great if I could have an example of how foldColumns can be used e.g. for summing over columns?

In general, is there some documentation source with examples? I am really new in numerics and C# in general.

Many thanks
Jun 10, 2014 at 2:06 PM
Edited Jun 10, 2014 at 2:08 PM
Note that FoldColumns folds over the columns themselves (each of them, individually), not over the column vectors. We should probably add the later as well.

Compute column sums with FoldColumns:
var m = Matrix<double>.Build.Random(4,4);
var columnSums = m.FoldColumns((sum, x) => sum+x, 0.0, Zeros.AllowSkip);
var rowSums = m.FoldRows((sum, x) => sum+x, 0.0, Zeros.AllowSkip);
Marked as answer by cdrnet on 6/17/2014 at 6:05 AM
Jun 10, 2014 at 2:12 PM
PS: let me know if you have an idea for better naming, to avoid confusing folding over column vectors v.s. folding over the values of each column.
Jun 10, 2014 at 4:42 PM

So in the function inside FoldColumns the only thing you have access to is a single cell value at a time.

Given that you keep FoldColumns as it is, perhaps FoldByColumns, FoldColumnsList, FoldColumnsSet, ApplyByColumns, ...
Jun 10, 2014 at 5:33 PM
Edited Jun 10, 2014 at 5:58 PM
Yes, exactly (and the state).

FoldColumns has just been added in v3.0.0-beta02, so we can still change it until v3 is released. What about the following naming?
  • FoldByColumn: renamed from FoldColumns; for each column, folds over the column values (lambda operates on values); result is effectively a row vector.
  • FoldColumns: folds over all column vectors (lambda operates on vectors); result is effectively a column vector.
Marked as answer by cdrnet on 6/17/2014 at 6:05 AM
Jun 16, 2014 at 10:37 AM
The proposed naming sounds good to me.