Your task is to write the shortest algorithm in a language of your choosing that accomplishes the following:
Given two matrices it must return the euclidean distance matrix. The euclidean distance between two points in the same coordinate system can be described by the following equation: \$D = \sqrt{ (x_2-x_1)^2 + (y_2-y_1)^2 + ... + (z_2-z_1)^2 }\$
The euclidean distance matrix is matrix the contains the euclidean distance between each point across both matrices. A little confusing if you're new to this idea, but it is described below with an example.
Below is an example:
a = [ 1.0 2.0 3.0;
-4.0 -5.0 -6.0;
7.0 8.0 9.0] #a 3x3 matrix
b = [1. 2. 3.] #a 1x3 matrix/vector
EuclideanDistance(a, b)
[ 0.0;
12.4499;
10.3923]] # a 3x1 matrix/vector see rules for relaxed scoring
In a typical matrix representation of data, or coordinates, the columns represent variables. These could by \$\text{X,Y,Z...,J}\$ coordinates. Most people think in terms of \$\text{XYZ}\$ for 3-D space(3 columns), or \$\text{XY}\$ for 2D space(2 columns). Each row of the matrix represents a different point, or object. The points are what is being compared.
Using the example, matrix b
is a single point at positions \$X= 1,Y = 2\$ and \$Z = 3\$. Matrix a
contains three points in the same set of coordinates. The first point in a
is the same as the point contained in b
so the euclidean distance is zero(the first row of the result).
Not to be confusing, but, the matrices can be of any size(provided they fit into RAM). So a 7 by 11 matrix being compared with a 5 by 11 matrix is possible. Instead of X,Y,Z we would then have 11 coordinates(or columns) in both input matrices. The output would either be a 7x5 or a 5x7 matrix (depending on what way the points are compared). Make sense? Please ask for further clarifications.
Here's a 4 dimensional matrix example
a = [ [1. 2. 3. 4.];
[ -4. -5. -6. -7. ];
[ 6. 7. 8. 9. ] ] #a 3x4 matrix
b = [ [1. 2. 3. 4.];
[1. 1. 1. 1.] ] #a 2x4 matrix
EuclideanDistance(a, b)
[ 0.0 3.74166;
16.6132 13.1909;
10.0 13.1909] #a 3x2 matrix
And another example for soundness:
a = [ [1. 2.];
[ 3.3 4.4 ] ] #a 2x2 matrix
b = [ [5.5 6.6];
[7. 8. ];
[9.9 10.1] ] #a 3x2 matrix
EuclideanDistance(a, b)
[6.43506 8.48528 12.0341;
3.11127 5.16236 8.72067] #a 2x3 matrix
Rules:
If this function is included in your base language you can use it. You can import the direct function to do this as well. But you sacrifice style and honor! You will not be evaluated on style or honor but your street cred. will be - your call :P .
Your submission should be evaluated in bytes. So save off your code to plain text and read the file size. Less bytes is best!
No printing necessary, just a function, lambda, whatever, that computes this operation.
Reasonable round-off error is fine, and the transpose of the correct solutions is also fine.
This must work for matrices!
Happy golfing!