1 /*
2 * Copyright (c) 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 "least_square_impl.h"
17 #include "matrix3.h"
18 #include "matrix4.h"
19 #include "common_utilities_hpp.h"
20
21 namespace OHOS::uitest {
GetLSMParams(std::vector<double> & params)22 bool LeastSquareImpl::GetLSMParams(std::vector<double>& params)
23 {
24 if (tVals_.size() <= ONE || (paramsNum_ != Matrix3::DIMENSION)) {
25 LOG_E("size is invalid, %{public}d, %{public}d", static_cast<int32_t>(tVals_.size()), paramsNum_);
26 return false;
27 }
28 params.resize(paramsNum_, ZERO);
29 if (isResolved_) {
30 params.assign(params_.begin(), params_.end());
31 return true;
32 }
33 auto countNum = std::min(countNum_, static_cast<int32_t>(std::distance(tVals_.begin(), tVals_.end())));
34 std::vector<double> xVals;
35 xVals.resize(countNum, ZERO);
36 std::vector<double> yVals;
37 yVals.resize(countNum, ZERO);
38 int32_t size = countNum - ONE;
39 for (auto iter = tVals_.rbegin(); iter != tVals_.rend(); iter++) {
40 xVals[size] = *iter;
41 size--;
42 if (size < ZERO) {
43 break;
44 }
45 }
46 size = countNum - ONE;
47 for (auto iter = pVals_.rbegin(); iter != pVals_.rend(); iter++) {
48 yVals[size] = *iter;
49 size--;
50 if (size < ZERO) {
51 break;
52 }
53 }
54 if (paramsNum_ == Matrix3::DIMENSION) {
55 MatrixN3 matrixn3 { countNum };
56 for (auto i = 0; i < countNum; i++) {
57 const auto& value = xVals[i];
58 matrixn3[i][TWO] = ONE;
59 matrixn3[i][ONE] = value;
60 matrixn3[i][ZERO] = value * value;
61 }
62 Matrix3 invert;
63 auto transpose = matrixn3.Transpose();
64 if (!(transpose * matrixn3).Invert(invert)) {
65 LOG_E("fail to invert");
66 return false;
67 }
68 auto matrix3n = invert * transpose;
69 auto ret = matrix3n.ScaleMapping(yVals, params);
70 if (ret) {
71 params_.assign(params.begin(), params.end());
72 isResolved_ = true;
73 }
74 return ret;
75 }
76 MatrixN4 matrixn4 { countNum };
77 for (auto i = 0; i < countNum; i++) {
78 const auto& value = xVals[i];
79 matrixn4[i][THREE] = ONE;
80 matrixn4[i][TWO] = value;
81 matrixn4[i][ONE] = value * value;
82 matrixn4[i][ZERO] = value * value * value;
83 }
84 auto transpose = matrixn4.Transpose();
85 auto inversMatrix4 = Matrix4::Invert(transpose * matrixn4);
86 auto matrix4n = inversMatrix4 * transpose;
87 auto ret = matrix4n.ScaleMapping(yVals, params);
88 if (ret) {
89 params_.assign(params.begin(), params.end());
90 isResolved_ = true;
91 }
92 return ret;
93 }
94 } // namespace OHOS::uitest
95