score:6

Accepted answer

edit: the code below will work, but it's not as readable as using an explicit method. linq is great where it definitely adds to readability... but this isn't one of those cases.

this is a briefer version of cms's answer - the extra let isn't required, and when you're just doing a projection it's simpler to just use dot notation:

int[] result = enumerable.range(0, a.length)
                         .select(i => a[i] * b[i])
                         .toarray();

an alternative is to use the form of select which takes an index:

int[] result = a.select((value, index) => value * b[index])
                .toarray();

score:0

check out this msdn article on the upcoming plinq (parallel linq). from the article, here is an example of using plinq to parallelize matrix multiplication:

void parmatrixmult(int size, double[,] m1, double[,] m2, double[,] result)
{
  parallel.for( 0, size, delegate(int i) {
    for (int j = 0; j < size; j++) {
      result[i, j] = 0;
      for (int k = 0; k < size; k++) {
        result[i, j] += m1[i, k] * m2[k, j];
      }
    }
  });
}

it's using linq and a lambda! and as a bonus it's spread across processors.

score:0

you can write a simple extension that works with matrices of any rank.

public static class twodimensionalarrayextensions
{
    public static int[][] multiplyby(this int[][] leftmatrix, int[][] rightmatrix)
    {
        if (leftmatrix[0].length != rightmatrix.length)
        {
            return null; // matrices are of incompatible dimensions
        }

        return leftmatrix.select( // goes through <leftmatrix matrix> row by row

                (leftmatrixrow, leftmatrixrowindexthatisnotused) =>

                    rightmatrix[0].select( // goes through first row of <rightmatrix> cell by cell

                            (rightfirstrow, rightmatrixcolumnindex) =>

                                rightmatrix
                                    .select(rightrow => rightrow[rightmatrixcolumnindex]) // selects column from <rightmatrix> for <rightmatrixcolumnindex>
                                    .zip(leftmatrixrow, (rowcell, columncell) => rowcell * columncell) // does scalar product
                                    .sum() // computes the sum of the products (rowcell * columncell) sequence.
                        )
                        .toarray() // the new cell within computed matrix
            )
            .toarray(); // the computed matrix itself
    }
}

here are some test values:

// test1
int[][] a = { new[] { 1, 2, 3 } };
int[][] b = { new[] { 1 }, new[] { 2 }, new[] { 3 } };
int[][] result = a.multiplyby(b);

// test2
int[][] a = { new[] { 1 }, new[] { 2 }, new[] { 3 } };
int[][] b = { new[] { 1, 2, 3 } };
int[][] result = a.multiplyby(b);

// test3
int[][] a = new int[][] { new[] { 1, 2 }, new[] { 2, 2 }, new[] { 3, 1 } };
int[][] b = new int[][] { new[] { 1, 1, 1 }, new[] { 2, 3, 2 } };
int[][] result = a.multiplyby(b);

score:0

if the right matrix is transposed beforehand, multiplication can be expressed more elegantly as follows.

int[][] multiply(int[][] left, int[][] right) =>
    left.select(lr => 
            right
                .select(rr => 
                    lr.zipped(rr, (l, r) => l * r).sum())
                .toarray())
        .toarray();

score:2

you can do something like this:

int[] a = {10, 20, 30};
int[] b = {2, 4, 10};

if (a.length == b.length)
{
  int[] result = (from i in enumerable.range(0, a.length)
          let operation = a[i]*b[i]
        select operation).toarray();
}

but i recommend you if you will work with matrices and more advanced mathematical topics to get a good math library, like nmath or search for a matrix class implementation, there are many out there...

score:2

there is nothing built in, but you can always write your own functions. the first one below is a simple extension method doing what you want. the second allows you to specify the function to apply:

class program
{
    public static void main(string[] args)
    {
        int[] a = { 10, 20, 30 };
        int[] b = { 2, 4, 10 };
        int[] c = a.matrixmultiply(b);
        int[] c2 = a.zip(b, (p1, p2) => p1 * p2);
    }
}

public static class extension
{
    public static int[] matrixmultiply(this int[] a, int[] b)
    {
        // todo: add guard conditions
        int[] c = new int[a.length];
        for (int x = 0; x < a.length; x++)
        {
            c[x] = a[x] * b[x];
        }
        return c;
    }

    public static r[] zip<a, b, r>(this a[] a, b[] b, func<a, b, r> func)
    {
        // todo: add guard conditions
        r[] result = new r[a.length];
        for (int x = 0; x < a.length; x++)
        {
            result[x] = func(a[x], b[x]);
        }
        return result;
    }
}

score:5

using the zip function (new to .net 4.0) details here:

int[] a = { 10, 20, 30 };
int[] b = { 2, 4, 10 };

int[] c = a.zip(b, (a1, b1) => a1 * b1).toarray();

until .net 4 comes out you can use the zip implementation from the link above.


Related Query

More Query from same tag