| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <exploragram/optimal_transport/linear_least_squares.h> | ||
| 41 | |||
| 42 | namespace GEO { | ||
| 43 | |||
| 44 | ✗ | LinearLeastSquares::LinearLeastSquares( | |
| 45 | index_t degree | ||
| 46 | ✗ | ) : | |
| 47 | ✗ | degree_(degree) | |
| 48 | { | ||
| 49 | ✗ | switch(degree_) { | |
| 50 | ✗ | case 1: | |
| 51 | ✗ | dim_ = 4; | |
| 52 | ✗ | break; | |
| 53 | ✗ | case 2: | |
| 54 | ✗ | dim_ = 10; | |
| 55 | ✗ | break; | |
| 56 | ✗ | default: | |
| 57 | ✗ | geo_assert_not_reached; | |
| 58 | } | ||
| 59 | ✗ | } | |
| 60 | |||
| 61 | ✗ | void LinearLeastSquares::begin() { | |
| 62 | ✗ | AtA_4_.load_zero(); | |
| 63 | ✗ | AtA_10_.load_zero(); | |
| 64 | ✗ | for(index_t i = 0; i < MAX_DIM; ++i) { | |
| 65 | ✗ | Atb_[i] = 0.0; | |
| 66 | } | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | |||
| 70 | ✗ | void LinearLeastSquares::end() { | |
| 71 | ✗ | switch(degree_) { | |
| 72 | ✗ | case 1: { | |
| 73 | ✗ | Matrix<4,double> M = AtA_4_.inverse(); | |
| 74 | ✗ | mult(M, Atb_, eqn_); | |
| 75 | ✗ | } break; | |
| 76 | ✗ | case 2: { | |
| 77 | ✗ | Matrix<10,double> M = AtA_10_.inverse(); | |
| 78 | ✗ | mult(M, Atb_, eqn_); | |
| 79 | ✗ | } break; | |
| 80 | ✗ | default: | |
| 81 | ✗ | geo_assert_not_reached; | |
| 82 | } | ||
| 83 | ✗ | } | |
| 84 | |||
| 85 | |||
| 86 | ✗ | void LinearLeastSquares::add_point(const double* p, double v) { | |
| 87 | ✗ | switch(degree_) { | |
| 88 | ✗ | case 1: | |
| 89 | ✗ | add_point_degree_1(p,v); | |
| 90 | ✗ | break; | |
| 91 | ✗ | case 2: | |
| 92 | ✗ | add_point_degree_2(p,v); | |
| 93 | ✗ | break; | |
| 94 | ✗ | default: | |
| 95 | ✗ | geo_assert_not_reached; | |
| 96 | } | ||
| 97 | ✗ | } | |
| 98 | |||
| 99 | |||
| 100 | ✗ | void LinearLeastSquares::add_point_degree_1(const double* p, double v) { | |
| 101 | ✗ | geo_debug_assert(degree_ == 1); | |
| 102 | double b[MAX_DIM]; | ||
| 103 | ✗ | eval_basis(p, b); | |
| 104 | ✗ | for(index_t i = 0; i < dim(); ++i) { | |
| 105 | ✗ | for(index_t j = 0; j < dim(); ++j) { | |
| 106 | ✗ | AtA_4_(i, j) += b[i] * b[j]; | |
| 107 | } | ||
| 108 | ✗ | Atb_[i] += b[i] * v; | |
| 109 | } | ||
| 110 | ✗ | } | |
| 111 | |||
| 112 | ✗ | void LinearLeastSquares::add_point_degree_2(const double* p, double v) { | |
| 113 | ✗ | geo_debug_assert(degree_ == 2); | |
| 114 | double b[MAX_DIM]; | ||
| 115 | ✗ | eval_basis(p, b); | |
| 116 | ✗ | for(index_t i = 0; i < dim(); ++i) { | |
| 117 | ✗ | for(index_t j = 0; j < dim(); ++j) { | |
| 118 | ✗ | AtA_10_(i, j) += b[i] * b[j]; | |
| 119 | } | ||
| 120 | ✗ | Atb_[i] += b[i] * v; | |
| 121 | } | ||
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | double LinearLeastSquares::eval(const double* p) const { | |
| 125 | double b[MAX_DIM]; | ||
| 126 | ✗ | for(index_t i = 0; i < MAX_DIM; ++i) { | |
| 127 | ✗ | b[i] = 0.0; | |
| 128 | } | ||
| 129 | ✗ | eval_basis(p, b); | |
| 130 | ✗ | double result = 0; | |
| 131 | ✗ | for(index_t i = 0; i < dim(); ++i) { | |
| 132 | ✗ | result += eqn_[i] * b[i]; | |
| 133 | } | ||
| 134 | ✗ | return result; | |
| 135 | } | ||
| 136 | |||
| 137 | ✗ | void LinearLeastSquares::eval_basis(const double* p, double* b) const { | |
| 138 | ✗ | double x = p[0]; | |
| 139 | ✗ | double y = p[1]; | |
| 140 | ✗ | double z = p[2]; | |
| 141 | ✗ | b[0] = 1.0; | |
| 142 | ✗ | b[1] = x; | |
| 143 | ✗ | b[2] = y; | |
| 144 | ✗ | b[3] = z; | |
| 145 | ✗ | if(degree_ >= 2) { | |
| 146 | ✗ | b[4] = x * x; | |
| 147 | ✗ | b[5] = y * y; | |
| 148 | ✗ | b[6] = z * z; | |
| 149 | ✗ | b[7] = x * y; | |
| 150 | ✗ | b[8] = y * z; | |
| 151 | ✗ | b[9] = z * x; | |
| 152 | } | ||
| 153 | ✗ | } | |
| 154 | |||
| 155 | |||
| 156 | } | ||
| 157 |