lunes, 21 de marzo de 2016

LINQ Recipe No. 2-1: Mathematics and Statistics - Compute the Dot Product of Two Vectors

Contents

1. Introduction
2. Key Words
3. Problem
4. Solution
5. Discussion
5.1 Dot product
5.2 Zip() standard query operator
6. Practice: Compute the Dot Product of Two Vectors
7. Conclusions
8. Literature & Links

1. Introduction

We have reached the second series of LINQ recipes which are dedicated to use many of the LINQ standard query operators (LSQO). These operators are designed for common mathematical and recursive series. The recipes will teach us how to solve different kinds of problems from diverse areas: mathematics and statistics, recursive series and patterns, collections, game design, and at the end of these recipes, we will discover and learn how to work with miscellaneous series. Each of those recipes will be executed as C# statements in LINQPad.


The first part will be focused on how to solve problems from mathematics and statistics.

2. Key Words

  • Dot product
  • Inner product
  • Mathematics
  • Scalar product
  • Standard query operator
  • Statistics

3. Problem

Write a function that computes the dot product -scalar product o inner product- of two vectors.

4. Solution

LINQ has the function Zip(). With this function we can perform some mathematical operation -i.e., sum, product, division, etcetera- on two members -or components- at the same index (or location) of a sequence.

5. Discussion

5.1 Dot product

The dot product (also know as scalar product or inner product) is defined as an algebraic operation which takes as input two equal-length sequences of numbers and generates as output a scalar (or number).

Its algebraic definition can be notated as follows
Dot product algebraic definition
Illustration 1. Dot product algebraic definition.
For example, with vectors 
Vector v

and 
Vector w
their dot product is equal to 
Dot product of v and w
Illustration 2. Dot product of v and w.

5.2 Zip() standard query operator

The Zip() standard query operator lets applies some specified function to a sequence of elements ("Enumerable.Zip", 2016).

This query operator can be applied to compute the dot product of two vectors as we will see in the next practice section.

In this simple example, we use Zip query operator to concatenate the elements of an integers array and the elements of a strings array:

int[] numbers = {1, 2, 3};
string[] words = {"One", "Two", "Three"};

var numbersAndWords = numbers.Zip(words, (first, second) => String.Format("{0} {1}", first, second));

foreach (var item in numbersAndWords)
{
Console.WriteLine(item);
}

Online execution (ideone.com): http://ideone.com/W3t6oT

Result: 

1 One
2 Two
3 Three

6. Practice: Compute the Dot Product of Two Vectors

(For the purpose of this practice, we need to have LINQPad installed on our system: follow this recipe to install it: LINQ Recipe No. 1-5: How to Install LINQPad?.)

It is necessary to choose C# Statement(s) as language in LINQPad to perform the dot product computation.
C# Statement(s) in LINQPad
Illustration 3. C# Statement(s) in LINQPad.
Once we have set up it as required, we write the following code:

int[] v = {1, 2, 3}; // First vector
int[] w = {4, 5, 6}; // Second vector

// Compute the dot product of v and w:
v.Zip(w, (v_i, w_i) => v_i * w_i).Dump("Dot Product");

Then, the next step is to execute it; and the resultant output will be 
Dot product v and w in LINQPad
Illustration 4. Dot product v and w in LINQPad.

7. Conclusions

We have learned how the Zip standard query operator lets us compute the dot product of two vectors. This query operator applies some operation -like addition, subtraction, multiplication, concatenation- on two sequences at the same index of their elements.

In next recipe we will explore how to generate Pythagorean triples.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Enumerable.Zip(TFirst, TSecond, TResult) Method (System.Linq) (2016, March 21). Retrieved from: https://msdn.microsoft.com/en-us/library/dd267698(v=vs.100).aspx


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.