[8401] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8401] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
[8323] | 21 |
|
---|
[8612] | 22 | using System;
|
---|
[8323] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 |
|
---|
[8371] | 26 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8612] | 27 | internal static class Util {
|
---|
[13981] | 28 | public static double ScalarProd(double[] v, double[] u) {
|
---|
| 29 | if (v.Length != u.Length) throw new InvalidOperationException();
|
---|
| 30 | double prod = 0.0;
|
---|
| 31 | for (int i = 0; i < v.Length; i++)
|
---|
| 32 | prod += v[i] * u[i];
|
---|
| 33 | return prod;
|
---|
[8323] | 34 | }
|
---|
| 35 |
|
---|
[8562] | 36 | public static double SqrDist(IEnumerable<double> x, IEnumerable<double> y) {
|
---|
| 37 | return x.Zip(y, (a, b) => (a - b) * (a - b)).Sum();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[8323] | 40 | public static double SqrDist(double x, double y) {
|
---|
| 41 | double d = x - y;
|
---|
[8463] | 42 | return d * d;
|
---|
[8323] | 43 | }
|
---|
| 44 |
|
---|
[13981] | 45 | public static double SqrDist(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) {
|
---|
| 46 | return SqrDist(x, i, x, j, columnIndices, scale);
|
---|
[8491] | 47 | }
|
---|
| 48 |
|
---|
[13981] | 49 | public static double SqrDist(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) {
|
---|
[8491] | 50 | double ss = 0.0;
|
---|
[13981] | 51 | if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)).ToArray();
|
---|
| 52 | for (int c = 0; c < columnIndices.Length; c++) {
|
---|
| 53 | var columnIndex = columnIndices[c];
|
---|
[8933] | 54 | double d = x[i, columnIndex] - xt[j, columnIndex];
|
---|
[8491] | 55 | ss += d * d;
|
---|
| 56 | }
|
---|
| 57 | return scale * scale * ss;
|
---|
| 58 | }
|
---|
[8562] | 59 |
|
---|
[13981] | 60 | public static double SqrDist(double[,] x, int i, int j, double[] scale, int[] columnIndices) {
|
---|
[8827] | 61 | return SqrDist(x, i, x, j, scale, columnIndices);
|
---|
[8491] | 62 | }
|
---|
| 63 |
|
---|
[13981] | 64 | public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) {
|
---|
[8491] | 65 | double ss = 0.0;
|
---|
[8827] | 66 | int scaleIndex = 0;
|
---|
[13981] | 67 | for (int c = 0; c < columnIndices.Length; c++) {
|
---|
| 68 | var columnIndex = columnIndices[c];
|
---|
[8933] | 69 | double d = x[i, columnIndex] - xt[j, columnIndex];
|
---|
[8827] | 70 | ss += d * d * scale[scaleIndex] * scale[scaleIndex];
|
---|
| 71 | scaleIndex++;
|
---|
[8491] | 72 | }
|
---|
[8933] | 73 | // must be at the end of scale after iterating over columnIndices
|
---|
| 74 | if (scaleIndex != scale.Length)
|
---|
| 75 | throw new ArgumentException("Lengths of scales and covariance functions does not match.");
|
---|
[8491] | 76 | return ss;
|
---|
| 77 | }
|
---|
[13981] | 78 | public static double ScalarProd(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) {
|
---|
| 79 | return ScalarProd(x, i, x, j, columnIndices, scale);
|
---|
[8562] | 80 | }
|
---|
[8491] | 81 |
|
---|
[13981] | 82 | public static double ScalarProd(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) {
|
---|
[8562] | 83 | double sum = 0.0;
|
---|
[13981] | 84 | for (int c = 0; c < columnIndices.Length; c++) {
|
---|
| 85 | var columnIndex = columnIndices[c];
|
---|
[8933] | 86 | sum += x[i, columnIndex] * xt[j, columnIndex];
|
---|
[8562] | 87 | }
|
---|
| 88 | return scale * scale * sum;
|
---|
| 89 | }
|
---|
[13981] | 90 | public static double ScalarProd(double[,] x, int i, int j, double[] scale, int[] columnIndices) {
|
---|
[8678] | 91 | return ScalarProd(x, i, x, j, scale, columnIndices);
|
---|
[8562] | 92 | }
|
---|
| 93 |
|
---|
[13981] | 94 | public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) {
|
---|
[8562] | 95 | double sum = 0.0;
|
---|
[8827] | 96 | int scaleIndex = 0;
|
---|
[13981] | 97 | for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) {
|
---|
| 98 | var columnIndex = columnIndices[c];
|
---|
[8933] | 99 | sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex];
|
---|
[8562] | 100 | }
|
---|
[8933] | 101 | // must be at the end of scale after iterating over columnIndices
|
---|
| 102 | if (scaleIndex != scale.Length)
|
---|
| 103 | throw new ArgumentException("Lengths of scales and covariance functions does not match.");
|
---|
| 104 |
|
---|
[8562] | 105 | return sum;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[8323] | 108 | public static IEnumerable<double> GetRow(double[,] x, int r) {
|
---|
| 109 | int cols = x.GetLength(1);
|
---|
[8982] | 110 | return GetRow(x, r, Enumerable.Range(0, cols));
|
---|
[8323] | 111 | }
|
---|
[8982] | 112 | public static IEnumerable<double> GetRow(double[,] x, int r, IEnumerable<int> columnIndices) {
|
---|
| 113 | return columnIndices.Select(c => x[r, c]);
|
---|
| 114 | }
|
---|
[8366] | 115 | public static IEnumerable<double> GetCol(double[,] x, int c) {
|
---|
| 116 | int rows = x.GetLength(0);
|
---|
| 117 | return Enumerable.Range(0, rows).Select(r => x[r, c]);
|
---|
| 118 | }
|
---|
[8323] | 119 | }
|
---|
| 120 | }
|
---|