cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Variable Type "Generator" in Function

sthomet
1-Visitor

Variable Type "Generator" in Function

Hey guys,

I'm trying to build a function which multiplies a vector with each column of a matrix and returns this 'multiplied' matrix.

However I get some errors I don't understand.

Error.png

I have the feeling that mathcad fails to cope with this "generator-type" (e.g. 0..6) inside functions. However it could be a usage error

The sheet is attached below, does anyone see what is wrong with this or knows an easier way to solve the problem?

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:sthomet)

The error message isn't "Shitty", but righteous. There are several problems with your program. First you are defining M2 as a range, then in the for-loop you are using this M2 in an augment statement (that requires a vector or matrix as its argument) and assigning the result of the augment back to M2....

Is this what you are looking for?

Success!
Luc

View solution in original post

12 REPLIES 12
LucMeekes
23-Emerald III
(To:sthomet)

The error message isn't "Shitty", but righteous. There are several problems with your program. First you are defining M2 as a range, then in the for-loop you are using this M2 in an augment statement (that requires a vector or matrix as its argument) and assigning the result of the augment back to M2....

Is this what you are looking for?

Success!
Luc

Thanks for the answer, I knew there was an easier solution...

But please note that using a range (or "generator" because that's what I meant) as a vector is normally fine:

Error.png

So I stand by my statement that this error is Error.png!

Werner_E
25-Diamond I
(To:sthomet)

> note that using a range (or "generator" because that's what I meant) as a vector is normally fine:

No, its not.

It may only look fine and ranges unfortunately look like vectors but they aren't. Ranges are iterators and should only be used for plotting, for-loops and indexing matrices (or vectors).

In your case you had used (without knowing, I guess) an undocumented trick: If you follow the range definition by a numeric evaluation (=), the range is converted to a vector.

So the r in your augment command is already a vector.

WE

LucMeekes
23-Emerald III
(To:sthomet)

What version of Prime are you using.

In Prime 3.1 (express) I can enter the range only as:

(So NOT without the comma), and yes, then it behaves as a vector as is shown by the subsequent evaluations and by:

Luc

Werner_E
25-Diamond I
(To:LucMeekes)

> I can enter the range only as:

No you can. Simply type    r:1..2

instead of the semicolon ";" we are used to from older versions, you have to type two ellipses ".."

> then it behaves as a vector

in fact it IS a true vector because of the inline evaluation after the defintion (undocumented trick).

And because we cannot make inline evaluation from within a program the trick can't work with ranges inside of programs.

WE

Yeah great "undocumented trick" which destroys any attempts of succesfull debugging! LucMeekesI'm using Prime 3.1 but (not express). You can delete the comma after that you've wrote it. But perhaps that's also some undocumented trick.

Werner_E
25-Diamond I
(To:sthomet)

However it could be a usage error

It is, as Luc already pointed out.

You wanted to create a dummy vector of the right size in the first line of your program but created a range variable. You can create that vector by simply assigning the last component any value.

You also had two errors (wrong indices) in your submatrix command.

Your routine works if you make the appropriate corrections

but nevertheless I would vote for Lucs appraoch.

WE

Error.png

The difference between vectors and ranges is not clear at all. It sometimes works and sometimes doesn't.

LucMeekes
23-Emerald III
(To:sthomet)

So now remove the '=' at the end of your definition of r and see what happens.

Werner_E
25-Diamond I
(To:sthomet)

See above my remarks about the undocumented trick above.

See also that it won't work without inline eval:

There is a logic in the subject range vs vector, but I agree that its quite tricky.

You may be interested in some papers written by Stuart quite a while ago. They still apply to Prime in most parts. See attachments.

WE

LucMeekes
23-Emerald III
(To:Werner_E)

Hm. In Prime 3.1 it seems they've done something about ranges. I checked the following to test for 'vectorness', can you find more tests?

Note that it all started with the definition r:1,2;2= (gives "r:=1,2..2=[1 above 2]").

Ah. I found it. it works as long as the '=' is at the end of the definition (of r) If I remove the '=', so my definition becomes:

r:1,2;2 (gives "r:=1,2..2") then the vector operations do NOT work anymore.

And the evaluation (the '=') you cannot do inside a program, that's why it can never work what Stefan tried.

Luc

Werner_E
25-Diamond I
(To:LucMeekes)

Yes, correct. Its the inline eval, which does the job,

You could also define r:=1..2 and then v:=r=

r will be a range and v a vector.

WE

Announcements

Top Tags