10
3
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!
3welcome to codegolf! i have a question. wikipedia describes the input as "a set of points". how are their coordinates encoded in the two matrices? – ngn – 2019-11-17T14:04:28.947
6
Banning built-in functions is generally discouraged. Also, this previous challege is very similar
– Luis Mendo – 2019-11-17T14:11:23.883Sorry everyone this is my first go at this! I'll clarify the post ASAP. – caseyk – 2019-11-17T14:31:25.883
Okay I tried to clarify more, and adopted the rules to be more flexible. Please let me know if anything is still unclear or goes against tradition :) – caseyk – 2019-11-17T14:44:05.470
@luis mendo - that previous challenge is similar but sufficiently different to me. This is an extension. We do not want to compare just 2 points, but a series of points across 2 sets. There are a lot of ways to accomplish this from brute force to elegant math! – caseyk – 2019-11-17T15:06:59.417
@caseyk i suggest removing the wikipedia link because it describes a single set of points and squared distances – ngn – 2019-11-17T15:45:55.807
About this challenge being an extension of the linked one: see here
– Luis Mendo – 2019-11-17T17:23:23.740@ngn - suggestion taken! – caseyk – 2019-11-17T17:54:45.590
@LuisMendo - I think this is more interesting then you give it credit for. If you'd like I can post a solution that shows that mathematically there is atleast one interesting way to go about doing this. – caseyk – 2019-11-17T17:55:48.200
You should provide a test case where $b$ is a matrix instead of a vector -- which I think is allowed by the challenge. – Arnauld – 2019-11-17T18:08:33.183
You got it Arnauld! – caseyk – 2019-11-17T18:09:07.627
Anyone know if I am allowed to submit my own answer despite having posted the question? It won't win by bytes but it's a fun way to look at the problem that might inspire others. – caseyk – 2019-11-18T00:29:05.023
@caseyk It's perfectly fine! Usually it is suggested to wait a day or so, which you have already done. – Stephen – 2019-11-18T16:33:21.317