Error in generating C/C++ codes: Cannot assign a complex value into a non-complex location
古いコメントを表示
Codegen is giving the same error for this line:
state.icaweights = V / sqrt(D) * V' * state.icaweights;
When I tried the codegen again by writing:
state.icaweights = complex(V / sqrt(D) * V' * state.icaweights);
OR
state.icaweights = double(V / sqrt(D) * V' * state.icaweights);
it still provided the same error. Can anyone please help in rectification of this error?
回答 (1 件)
Walter Roberson
2021 年 9 月 13 日
1 投票
state.icaweights has been created as non-complex, but either V is complex or D is suspected of being negative so code generation figures that the right hand side is generating a complex value.
If you are expecting complex values, then you need to initialize state.icaweights to complex.
For example if you initialized it to 0 before, then initialize it to complex(0,0)
6 件のコメント
PIYUSH SWAMI
2021 年 9 月 13 日
編集済み: PIYUSH SWAMI
2021 年 9 月 14 日
Walter Roberson
2021 年 9 月 14 日
Are you taking roots of a polynomial of degree more than 2 ? And you are expecting real-valued outputs ?
roots here could possibly include eigenvalues for this purpose.
PIYUSH SWAMI
2021 年 9 月 16 日
編集済み: Walter Roberson
2021 年 9 月 16 日
Walter Roberson
2021 年 9 月 16 日
If your system is larger than 2 x 2, then the eigenvalues involve numeric solutions of polynomials of degree 3 or higher. There are exact solutions for degree 3 and 4, but both of those involve intermediate complex values that where algebraically the complex portions happen to cancel out in cases that the eigenvalues should be real-valued.
However, when you generate code, you are not using the exact same eigenvalue code as is used by regular MATLAB: regular MATLAB calls upon high-performance MKL (Math Kernel Library) or LAPACK external libraries, And because the two cases use different code, you can get slightly different rounding, leading to the complex parts not exactly balancing. Those last few bits of difference can then get multiplied by large coefficients, so the overall differences can end up large.
Even if the exact same code were being used, MKL and LAPACK are compiled by vendors for high performance, and if you do not use exactly the same compiler flags, you could get differences in outcomes.
So... try taking real(V) and real(D) (but print max(abs(imag(V))) and max(abs(imag(D))) to see how much you are throwing away.) .
PIYUSH SWAMI
2021 年 9 月 17 日
編集済み: Walter Roberson
2021 年 9 月 18 日
Walter Roberson
2021 年 9 月 18 日
Compute the whole thing as complex, assigned into a local variable. Extract real() part of that to assign into icaweights .
Simulink appears to be predicting that Dreal is potentially negative. There might be Good Reasons in your case why it cannot be negative for you, but Simulink either does not have sufficient evidence of that or else Simulink just was not able to follow the flow well enough to prove it.
カテゴリ
ヘルプ センター および File Exchange で Algorithm Design Basics についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!