Post Reply 
Return a vector from a function?
07-06-2015, 12:55 AM
Post: #1
Return a vector from a function?
How can I create a user defined function to map two variables into a vector on the prime?

i.e. something along the lines of:

defvect(angle, magnitude):=[magnitude*cos(angle), magnitude*sin(angle)]

but that works.
Find all posts by this user
Quote this message in a reply
07-06-2015, 07:44 AM
Post: #2
RE: Return a vector from a function?
OK so I solved my task by making it a program

Code:

EXPORT DEFVECT(mag,ang)
BEGIN
   RETURN([mag*COS(ang),mag*SIN(ang)]);
END;

But that really feels like tapping an exposed nail head with a sledge hammer. Is there no way to return a vector or matrix from the 'Define' window?
Find all posts by this user
Quote this message in a reply
07-06-2015, 09:45 AM (This post was last modified: 07-06-2015 09:46 AM by Didier Lachieze.)
Post: #3
RE: Return a vector from a function?
Here is a way to return a vector from a User Defined function (a bit cumbersome but it seems to work…):

   

But in your case it's much more simpler to use the existing rectangular_coordinates() function:

   
Find all posts by this user
Quote this message in a reply
07-06-2015, 10:21 AM (This post was last modified: 07-06-2015 10:25 AM by DrD.)
Post: #4
RE: Return a vector from a function?
By using a list first, try this function, which will create the variables M0, mag, and ang. (Uncheck the [M0]):

[Shift] [Define]
NAME: defvect
Function: M0:=list2mat({mag*COS(ang), mag*SIN(ang)})

M0:[ ] mag:[] ang:[ ]

oops, I see Didier beat me to it!
Find all posts by this user
Quote this message in a reply
07-06-2015, 12:00 PM
Post: #5
RE: Return a vector from a function?
Thanks to both of you for the solution and pointing out the built in function.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)