1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "base/geometry/least_square_impl.h"
17 
18 #include "base/geometry/matrix3.h"
19 #include "base/geometry/matrix4.h"
20 
21 namespace OHOS::Ace {
GetLeastSquareParams(std::vector<double> & params)22 bool LeastSquareImpl::GetLeastSquareParams(std::vector<double>& params)
23 {
24     if (xVals_.size() <= 1 || ((paramsNum_ != Matrix3::DIMENSION) && (paramsNum_ != Matrix4::DIMENSION))) {
25         return false;
26     }
27     params.resize(paramsNum_, 0);
28     if (isResolved_) {
29         params.assign(params_.begin(), params_.end());
30         return true;
31     }
32     auto countNum = std::min(countNum_, static_cast<int32_t>(std::distance(xVals_.begin(), xVals_.end())));
33     std::vector<double> xVals;
34     xVals.resize(countNum, 0);
35     std::vector<double> yVals;
36     yVals.resize(countNum, 0);
37     int32_t size = countNum - 1;
38     for (auto iter = xVals_.rbegin(); iter != xVals_.rend(); iter++) {
39         xVals[size] = *iter;
40         size--;
41         if (size < 0) {
42             break;
43         }
44     }
45     size = countNum - 1;
46     for (auto iter = yVals_.rbegin(); iter != yVals_.rend(); iter++) {
47         yVals[size] = *iter;
48         size--;
49         if (size < 0) {
50             break;
51         }
52     }
53     if (paramsNum_ == Matrix3::DIMENSION) {
54         MatrixN3 matrixn3 { countNum };
55         for (auto i = 0; i < countNum; i++) {
56             const auto& value = xVals[i];
57             matrixn3[i][2] = 1;
58             matrixn3[i][1] = value;
59             matrixn3[i][0] = value * value;
60         }
61         Matrix3 invert;
62         auto transpose = matrixn3.Transpose();
63         if (!(transpose * matrixn3).Invert(invert)) {
64             return false;
65         }
66         auto matrix3n = invert * transpose;
67         auto ret = matrix3n.MapScalars(yVals, params);
68         if (ret) {
69             params_.assign(params.begin(), params.end());
70             isResolved_ = true;
71         }
72         return ret;
73     }
74     MatrixN4 matrixn4 { countNum };
75     for (auto i = 0; i < countNum; i++) {
76         const auto& value = xVals[i];
77         matrixn4[i][3] = 1;
78         matrixn4[i][2] = value;
79         matrixn4[i][1] = value * value;
80         matrixn4[i][0] = value * value * value;
81     }
82     auto transpose = matrixn4.Transpose();
83     auto inversMatrix4 = Matrix4::Invert(transpose * matrixn4);
84     auto matrix4n = inversMatrix4 * transpose;
85     auto ret = matrix4n.MapScalars(yVals, params);
86     if (ret) {
87         params_.assign(params.begin(), params.end());
88         isResolved_ = true;
89     }
90     return ret;
91 }
92 } // namespace OHOS::Ace
93