2

After some research I have settled on ILNumerics for a Linear Algebra package in C#.

However I have having some issues working on ranges of the vector. I would like to modify the values in a Vector with a type of moving window, applying a function on the values in this window or range.

Any ideas on how to accomplish this? I cannot find how to do this in the documentation.

This is the kind of operation I would like to do:

        ILArray<double> vec = ILMath.array(new [] {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0});
        Console.WriteLine(vec);
        // create a vector range from index 3-5
        var range = vec[2, 5];
        Console.WriteLine(range);
        // modify all values in range
        for (int i = 0; i < range.Length; i++)
            range[i] += 10.0;
        Console.WriteLine(range);
        // view modified original vector
        Console.WriteLine(vec);

This will not work, as the range incorrect and the vector cannot be written to using indexing.

Thanks.

C Mars
  • 2,909
  • 4
  • 21
  • 30

1 Answers1

1

I'm a bit unsure if I got this correctly. But you certainly can modify ILArray. Just make sure, you understand the basics for working with ILArray and how to handle the different array types. Especially, prevent from using var in conjunction with ILArray!

Read about the core array features:

http://ilnumerics.net/docs-core.html

Read how to create functions and handle the different ILNumerics array types:

http://ilnumerics.net/GeneralRules.html

I have modified your example. If this is not what you need, please comment on it:

ILArray<double> vec = ILMath.array(new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 });
Console.WriteLine(vec);
// create a vector range from index 3-5
ILArray<int> range = array<int>(2,3,4,5);
Console.WriteLine(range);
// modify all values in range
for (int i = 0; i < 3; i++) // make tree steps. Modify as needed!
    vec[range + i] = vec[range + i] + i;
Console.WriteLine(range);
// view modified original vector
Console.WriteLine(vec);

@Edit: From the comments... this may bring us closer to what you are trying to achieve?

for (int i = 0; i < 3; i++) // make tree steps. Modify as needed!
    // dynamic subarrays using ILMath.r() 
    vec[r(i,i+5)] = vec[r(i,i+5)] + ... ;

Note that you have to use ILMath.r(..) instead of r(..) if your code is not defined in a class which derives from ILMath. Note further that subarray range definitions with r() can be combined with string definitions arbitrarly. This could help to translate it to cases, where matrices or n-dim arrays are involved.

Haymo Kutschbach
  • 3,322
  • 1
  • 17
  • 25
  • Thanks for your answer, the example is helpful. I see my mistake with the var, even though I have read these documents before. I was actually looking for a way to operate on a range of the vector in a similar way to Boost range : http://www.boost.org/doc/libs/1_41_0/libs/numeric/ublas/doc/range.htm - is this the best way? – C Mars May 23 '14 at 08:19
  • Looking back, the example was not really meant to compile - it was more intended as pseudo code. If the solution is to define an array of indices like this, then loop through the values it seems to be moving more away from vector operations - so there is little point in converting it. Thanks. – C Mars May 23 '14 at 08:45
  • SO wants you to upvote and mark useful answers instead of saying 'Thanks!'. But I am glad you find it useful. I've edited my answer. – Haymo Kutschbach May 23 '14 at 14:01
  • Ok I will avoid the word 'thanks' in future to save confusion! From what I can tell this does not answer my question. If you see my original post I would like to **apply a function on a range in a vector** . If you can apply the function like this: `vec[r(i,i+5)] = vec[r(i,i+5)] + ... ;` why are you putting it in a loop? Also does this act on the original vector or create a copy of the subarray? – C Mars May 23 '14 at 15:46
  • Yes you can modify the vector via subarray expressions on the left side of an equation. The loop was only because you were refering to a 'moving window'. – Haymo Kutschbach May 23 '14 at 20:34