TRANSACTIONS IN VECTORS C # USING LAMBDA EXPRESSION -2-

TRANSACTIONS IN VECTORS C # USING LAMBDA EXPRESSION -2-

 

By Mohamed Ali Ettougourti

 

We can go even further in our operation of the new programming tool that C # offers to us : the lambda expression.

(See previous article).

One can for example add a unique value to different values of a vector

x = x.Select (b => b = + 0.3) .ToArray ();

Or subtract a given value

x = x.Select (b => b – = 0.3) .ToArray ();

You guessed all arithmetic operations are possible:

The multiplication:

x = x.Select (b => b * = 0.3) .ToArray ();

The division:

x.Select x = (b => b / = 0.3) .ToArray ();

The precautions are still required for the division by zero which gives us « infinity » as a result.

The absolute values:

x = x.Select (b => Math.Abs (b)) ToArray ();

the elevation to power:

x2 = k2.Select (b => Math.pow (b, 2)) ToArray ();

To raise the k2 values, an array of type int, to the power of 2, however, we have used an array of double x2 such as an destination array : Math.pow returns indeed a double value.

These precautions taken into account all basic operations seem possible with the expression Lambda on a vector once and without iteration loops.

Appetite comes with eating nothing can now halt our momentum and we are entitled to ask whether other finer operations cannot be carried out on vectors.

C # we offer on several instructions to assist us and make our job even easier.

Thus it is possible to eliminate duplicates from a vector.

To do this we use the function or the « distinct » method.

First we initialize an array of integers with several duplicates example:

int [] k=  {2,1,2,3,5,3,4,1,6,7,8,2,1,0,8;}

if one uses distinct () we will see one value duplicates 2.3, 8, etc.   retained and array shrink from 15 items to just nine items.

k2 = {2,1,3,5,4,6,7,8,0};

I see coming your objection no lambda sign in distinct method, why invoke it?

You are right. This is why I  invoke  another useful method that requires this time the use of the lambda expression.

The method used to sort the table in ascending order:

k2 = k2.OrderBy (b => b) .ToArray ();

 Inspecting k2 array we see that the zero initiated at the end of the table now takes first place and that all values are ordered from smallest to largest.

k2 = {0,2,1,3,5,4,6,7,8};

You do not want an ascending order?  The same lambda expression allows us to sort the table in descending order from largest to smallest

k2 = k2.OrderByDescending (b => b) .ToArray ();

And voila:

k2 = {8,7,6,5,4,3,2,1,0};

To do so complicated that we can make it simple we can obviously order the values of an array  by ascending  order then “simply”  reverse the array using the reverse function or method.

k2.Reverse ();

But as you see the lambda expression is not in the reverse instruction.

If you insist on using the lambda expression in reverse method you will be quickly delivered to the order by the compiler on error type « can not convert lambda expression to type of array Because it is not a delegate type ‘.

This is not very clear but basically the compiler refuses to go: his desires, as everyone knows, are orders.

The most interesting is obviously to work on multiple vectors.

To do this we will initiate a second vector called   x2  of type double with random values using the random number generator t previously created.

double [] = x2 dual new [100];

x2 = x2.Select (b => b = t.NextDouble ()) ToArray ().

To add the values of the two vectors so we find our iterator variable, index or next or any other fancy name. (See previous article)

If we choose the vector x2 as destination vector line of code should be written as follows:

x.Select x2 = ((b, next) => b + x2.ElementAt (next)) ToArray ().

Note that we can reference the element in x2 directly with braces by putting a code in this form:

x.Select x2 = ((b, next) => b + x2 [next]) ToArray ().

It is also unusual with the same ease that we can subtract a vector of another multiply two vectors or divide one by the other.

x.Select x2 = ((b, next) => b – x2 [next]) ToArray ().

x.Select x2 = ((b, next) => b * x 2 [next]) ToArray ().

x.Select x2 = ((b, next) => b / x2 [next]) ToArray ().

it is also possible to perform operations on several arrays:

x.Select x2 = ((b, next) => b + x2 [next] * k2 [next]) ToArray ().

You tested this last line of code and have found that it does not work?

So much for me, I forgot to tell you about one important detail: the arrays should be the same size if not   exception   is raised by the program : « out of range » .

(18)