Opened 16 years ago
Last modified 16 years ago
#1146 closed defect (fixed)
Problem with [:] in arrays and matrixes (from Mathcore)
| Reported by: | Jan Brugård | Owned by: | Jan Brugård |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | Version: | ||
| Keywords: | Cc: | Jan Brugård, |
Description
See the model:
function z input Real B[:,:]; output Real A[size(B, 1),size(B, 2)]=zeros(size(B, 1), size(B, 2)); end z;
The error messages are:
Check of z failed Error: Wrong type or wrong number of arguments to zeros(size(B, 1), size(B, 2))'.
Functions like these are used in Modelica.Math.Matrices.singularValues.
There are problems with the colon dimensions [:] in many classes and I think a lot of tickets are written on different models that doesn't work because of this.
Change History (5)
comment:1 by , 16 years ago
comment:2 by , 16 years ago
OpenModelica doesn't support defining the output
for a function at the same time you are defining
the output component.
The setting of the output needs to be moved into an algorithm section.
comment:3 by , 16 years ago
Doing that does not help as far as I can see. I changed the model according your comment:
function singularValues "Compute singular values and left and right singular vectors"
extends Modelica.Icons.Function;
input Real A[:,:] "Matrix";
output Real sigma[min(size(A, 1), size(A, 2))] "Singular values";
output Real U[size(A, 1),size(A, 1)] "Left orthogonal matrix";
output Real VT[size(A, 2),size(A, 2)] "Transposed right orthogonal matrix ";
annotation(preferedView="info", Documentation(info="<HTML>
<h3><font color=\"#008000\">Syntax</font></h3>
<blockquote><pre>
sigma = Matrices.<b>singularValues</b>(A);
(sigma, U, VT) = Matrices.<b>singularValues</b>(A);
</pre></blockquote>
<h3><font color=\"#008000\">Description</font></h3>
<p>
This function computes the singular values and optionally the
singular vectors of matrix A. Basically the singular
value decomposition of A is computed, i.e.,
</p>
<blockquote><pre>
<b>A</b> = <b>U</b> <b><font face=\"Symbol\">S</font></b> <b>V</b><sup>T</sup>
= U*Sigma*VT
</blockquote></pre>
<p>
where <b>U </b>and <b>V</b> are orthogonal matrices (<b>UU</b><sup>T</sup>=<b>I,
</b><b>VV</b><sup>T</sup>=<b>I</b>). <b><font face=\"Symbol\">S
</font></b> = diag(<font face=\"Symbol\">s</font><sub>i</sub>)
has the same size as matrix A with nonnegative diagonal elements
in decreasing order and with all other elements zero
(<font face=\"Symbol\">s</font><sub>1</sub> is the largest element). The function
returns the singular values <font face=\"Symbol\">s</font><sub>i</sub>
in vector <tt>sigma</tt> and the orthogonal matrices in
matrices <tt>U</tt> and <tt>V</tt>.
</p>
<h3><font color=\"#008000\">Example</font></h3>
<blockquote><pre>
A = [1, 2, 3, 4;
3, 4, 5, -2;
-1, 2, -3, 5];
(sigma, U, VT) = singularValues(A);
results in:
sigma = {8.33, 6.94, 2.31};
i.e.
Sigma = [8.33, 0, 0, 0;
0, 6.94, 0, 0;
0, 0, 2.31, 0]
</pre></blockquote>
<h3><font color=\"#008000\">See also</font></h3>
<a href=\"Modelica:Modelica.Math.Matrices.eigenValues\">Matrices.eigenValues</a>
</HTML>"), Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
protected
Integer info;
Integer n=min(size(A, 1), size(A, 2)) "Number of singular values";
algorithm
U:=zeros(size(A, 1), size(A, 1)) "Left orthogonal matrix";
VT:=zeros(size(A, 2), size(A, 2)) "Transposed right orthogonal matrix ";
(sigma,U,VT,info):=Matrices.LAPACK.dgesvd(A);
assert(info == 0, "The numerical algorithm to compute the
singular value decomposition did not converge");
end singularValues;
but still get the same error:
[81] 16:21:34 Validation of class Modelica.Math.Matrices.singularValues Check of Modelica.Math.Matrices.singularValues failed Error: Wrong type or wrong number of arguments to zeros(size(A, 1), size(A, 1))'.
comment:4 by , 16 years ago
Ok, this is an additional problem with zeros()
function that doesn't support multiple dimensions
I think. I'll check that and come back with another
comment.
comment:5 by , 16 years ago
Fixed in revision 5093, see testcase FillSize. The problem was that elabBuiltinFill didn't handle cases where it couldn't constant evaluate the arguments to fill (zeros is just a special case of fill).

http://intranet/trac/mathmodelica/ticket/2615