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 "common_utilities_hpp.h"
19
20 namespace OHOS::uitest {
GetLSMParams(std::vector<double> & params)21 bool LeastSquareImpl::GetLSMParams(std::vector<double>& params)
22 {
23 if (tVals_.size() <= ONE || (paramsNum_ != Matrix3::DIMENSION)) {
24 LOG_E("size is invalid, %{public}d, %{public}d", static_cast<int32_t>(tVals_.size()), paramsNum_);
25 return false;
26 }
27 params.resize(paramsNum_, ZERO);
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(tVals_.begin(), tVals_.end())));
33 std::vector<double> xVals;
34 xVals.resize(countNum, ZERO);
35 std::vector<double> yVals;
36 yVals.resize(countNum, ZERO);
37 int32_t size = countNum - ONE;
38 for (auto iter = tVals_.rbegin(); iter != tVals_.rend(); iter++) {
39 xVals[size] = *iter;
40 size--;
41 if (size < ZERO) {
42 break;
43 }
44 }
45 size = countNum - ONE;
46 for (auto iter = pVals_.rbegin(); iter != pVals_.rend(); iter++) {
47 yVals[size] = *iter;
48 size--;
49 if (size < ZERO) {
50 break;
51 }
52 }
53 if (paramsNum_ == Matrix3::DIMENSION) {
54 LOG_D("MatrixN3_USING");
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 LOG_D("MatrixN3_USING_Finished");
75 return ret;
76 }
77 LOG_E("MatrixN4_USING_NEED");
78 return false;
79 }
80 } // namespace OHOS::uitest
81