Pia Medina

30 Reputation

One Badge

9 years, 26 days

MaplePrimes Activity


These are Posts that have been published by Pia Medina

A wealth of knowledge is on display in MaplePrimes as our contributors share their expertise and step up to answer others’ queries. This post picks out one such response and further elucidates the answers to the posted question. I hope these explanations appeal to those of our readers who might not be familiar with the techniques embedded in the original responses.

The Question: Transforming functions to names

Bendesarts wanted to know how to make programmatic changes to characters in a list. He wrote:

I have this list :

T:=[alpha(t),beta(t)]

I would like to create this list automatically:

Tmod:=[alpha_,beta_]

In other words, how can I remove the 3 characters "(t)" and replace it by "_"

Do you have ideas to do so ?

Thanks a lot for your help

Joe Riel provided a complete answer that had three different approaches. He wrote:

Map, over the list, a procedure that extracts the name of the function and catenates an underscore to it. The function name is extracted via the op procedure, e.g. op(0,f(a,b,c)) evaluates to f. Thus 

map(f->cat(op(0,f),_),T);

Note that this fails if the name of a function is indexed, e.g. f[2](a). No error is generated but the catenation doesn't evaluate to a symbol. Presumably that isn't an issue here.  One way to handle that case is to first convert the indexed name to a symbol, then catenate the underscore.  So a more robust version is

map(f->cat(convert(op(0,f),'symbol'),_),T);

However, if you are actually dealing with indexed names you might want a different result. Another way to do the conversion, and combine it with the catenation, is to use nprintf, which generates a name (symbol). Thus

map(f -> nprintf("%a_", op(0,f)),T);

 

Let’s discuss each approach by understanding the definitions and functionalities of the commands used. 

The map command, map(fcn, expr, arg1, ..., argN) applies a procedure or name, fcn, to the operands or elements of an expression, expr. The result of a call to map is a copy of expr with the ith operand of expr replaced by the result of applying fcn to the ith operand.  This concept is easier to grasp by looking at a few examples related to the usage of map in this question.

Example 1.  map(x-> x2,x+y)         returns     x2+y2                    

Example 2. map(a -> a-b, sin(x))    returns     sin(x-b)

 

The cat function, cat(a,b,c,…), is commonly used to concatenate (or join) string and names together. This function’s parameters: a,b,c…, can be any expressions.

Example 1. cat(a,2)                      returns     a2

Example 2.  cat(“a”,3,sin(x))          returns    “a3sin(x)”

 

The op function, op(i..j,e), extracts operands from an expression. The parameters i and j are the integers indicating positions of the operands and e is the expression. For functions, as in this example, op(0,e) is the name of the function.

Example 1.  op(0,alpha(t))            returns   the symbol alpha

Example 2.  op(0, sin(x))              returns    sin

 

Now analyzing Joe Riel's code will be easier.

  1. map(f->cat(op(0,f),_),T);

In this approach Joe is extracting the name of the functions, alpha and beta, and then concatenating it to the underscore symbol. Then using the mapping function he applies the previous procedure to the list T.

  1. map(f->cat(convert(op(0,f),'symbol'),_),T);

This approach is a lot similar to the previous one, but he added the convert function in case the function inside of map was indexed. Convert(expr, form, arg3,..), is used to change an expression from one form to another. In this example op(0,f) has been changed from type name to type symbol.

  1. map(f -> nprintf("%a_", op(0,f)),T);

Again this is a similar approach but it uses nprintf. This command, nprintf(fmt,x1,..xn), is based on a C standard library command of the same name. It uses the format specifications in the fmt string to format and writes the expression into a Maple symbol, which is returned. In this example the format specified is the algebraic format “%a”.

 

This blog was written by Maplesoft’s intern Pia under the supervision of Dr. Robert Lopez. We both hope that you find this useful. If there is a particular question on MaplePrimes that you would like further explained, please let us know. 

A wealth of knowledge is on display in MaplePrimes as our contributors share their expertise and step up to answer others’ queries. This post picks out one such response and further elucidates the answers to the posted question. I hope these explanations appeal to those of our readers who might not be familiar with the techniques embedded in the original responses.

The Question: How to sketch a line in space?

vahid65 wanted to know how to sketch the line with equation (x-2)/3 = (y-1)/4 = (z-3)/3 in a three-dimensional space.

This question was answered using two different approaches that we will discuss.

The first approach, given by Preben Alsholm, suggested using these commands: solve, subs, and plots:-spacecurve.

Preben provided the following lines of code:

  1. {(x-2)/3 , (y-1)/4 , (z-3)/3} =~t;
  2. solve(%,{x,y,z});
  3. L:=subs(%,[x,y,z]);
  4. plots:-spacecurve(L,t=-5..5,thickness=3,color=red);

The first line sets the three expressions equal to t using the element wise operator =~.  This distributes the operation of equality over the elements of the set of expressions, forming a set of equations.

Result-1: {(x-2)/3=t, (y-1)/4=t, (z-3)/3=t}

The second line invokes the solve command. The solve command, solve(equations, variables), solves one or more equations or inequalities for their unknowns. So in this line, this command was used to solve each expression for its corresponding unknown.

Result-2: {(x= 2+3t, y=1+4t, z= 3+3t}

You may have noticed that the % symbol is used within the command. This symbol is referring to the equation label that was created chronologically last. 

The third line uses the subs command. The subs command, subs(x=a,expr), substitutes “a” for “x” in the expression expr. In this case, “a” is the reference to the equation label referenced by (%) and expr is the sequence of variables x,y,z. The square brackets around expr forms a list, and the replacement imposed by the subs command replaces each of the three names x, y, and z with their equivalents in the set of equations returned by the solve command.

Result-3: [2+3t,1+4t, 3+3t ]

Finally the last line uses the plots:-spacecurve function.  This function, spacecurve(sc,r,opts),  graphs a parametrically defined curve in three-dimensional Cartesian space. 

In this example the first parameter, sc, is replaced by L since L has the properties of a list. The parameter  r is an equation containing the parameter name and the parameter range; here it is the equation t=-5..5. Last but not least, the opts parameter (which is optional) gives the user the opportunity to specify the optional properties of the graph, properties such as thickness and color of the space curve. 

Result 4: 

 

Another contributer, Carl Love, suggested that these commands could be combined using the zip function, zip( f, u, v), which is an unusual application of it. The zip command applies elementwise to corresponding members of the two lists u and v, the binary function f, creating a new list r, whose members are f(uk,vk).

Carl provided the following code:

plots:-spacecurve(  zip(solve, [(x-2)/3 , (y-1)/4 , (z-3)/3] =~ t, [x,y,z]),  t= -5..5, thickness= 3, color= red, axes= normal);

 

In this case zip is applying solve to u, a list of equations, and v, a list of variables. The equations in u are those in Result-1. The zip function returns the solution of each equation for the indicated variable, that is, the new list r is the list in Result-3. This list is the first argument of plots:-spacecurve. Finally, the parameter range and opts are up to the user’s preferences.

A second approach was suggested by Kitonum. The difference between this approach and the first one is that here we are not using the solve command. Instead, we are treating the line as an intersection of two planes.

Kitonum's code is as follows:

  1. L := [(x-2)*(1/3), (y-1)*(1/4), (z-3)*(1/3)]:
  2. plots[intersectplot](L[1]-L[2], L[2]-L[3], x = -4 .. 4, y = -4 .. 4, z = -4 .. 4, linestyle = 1, thickness = 3, axes = normal, orientation = [60, 75], view = [-1 .. 3, -4 .. 2, -1 .. 4]);

The first statement initializes a list L that contains all the desired expressions in the given symmetric form of the line.

In the second statement Kionum used the plots[intersectplot] command. This command graphs, in three-dimensional Cartesian space, the intersection of a pair of surfaces. This example uses the calling sequence: intersectplot(expr1, expr2, x=a..b, y=c..d, z= e..f, options)

What are expr1 and expr2 used in Kitonum’s solution? The pairwise differences of L[1] and L[2], and L[2] and L[3] are planes, the intersection of which is the desired line in space.

expr1 = L[1] – L[2] = (x-2)*(1/3)- (y-1)*(1/4)

expr2= L[2] - L[3] = (y-1)*(1/4)-(z-3)*(1/3)

The variables x,y, and z are the coordinate names and define the axes, whereas a, b, c, d, e, f define the ranges. In this case they were taken as -4 or 4.

In the options parameter Kitonum used linestyle, thickness, axes, orientation, and  view. The help page “plot3d,option” details these and other options that apply.

 

This blog was written by our intern Pia under the supervision of Dr. Robert Lopez. We both hope that you find this useful. If there is a particular question on MaplePrimes that you would like further explained, please let us know. 

A wealth of knowledge is on display in MaplePrimes as our contributors share their expertise and step up to answer others’ queries. This post picks out one such response and further elucidates the answers to the posted question. I hope these explanations appeal to those of our readers who might not be familiar with the techniques embedded in the original responses.

The Question: Variable Identification

Don Carota wanted to know the best approach for finding variables that are not assigned to a value within an equation. He wrote:

I got a set of equations to solve, like this one:

eq[1]:=W[1,0]*(a*HRa[1,0]+b*ga[1,0]+c)=d*SR[1,1]/grid

a,b,c,d are numbers, like 2.0458 and so on.

When I want to solve the set, I need to tell Maple the command solve:

solve( {seq(eq[i],i=1..N)},{variables});  (N is an integer of course)

To set the variables, one must check each equation to write: {W[1,0],HRa[1,0],ga[1,0]...} and so on.

I know that I can use the command is(variable,assignable) to check if a variable has not a value assigned already and, according to true/false I can construct the set {variables} and solve the set of equations.

That´s an easy solution if I need to check variables under a certain pattern, like: X[1], X[2], X[3] since I can create a loop and check them one after the other. But my problem is that I have different names for variables or that variables change to assignable from assigned according to other conditions, so I can never be sure if W[1,0] is going to be a variable to compute in all steps instead of SR[1,1].

for example:

if a>3 then
SR[1,1]:=1/2;
else
W[1,0]:=300:
end if;

So, when I need to type solve, the {variables} part is different according to each case. Is there any command that allows me to insert an expression and Maple can return me the variables or parameters in the expression that are not numeric already?

(note that the link added to the is command above was added by me, not the original author)

dharr and Carl Love provided solutions that use the indets command.

The code provided by dharr is as follow:

  1. indets(eq[1],name);

Result: gives the indeterminates: {a, b, c, d, HRa[1, 0], SR[1, 1], W[1, 0], ga[1, 0]}

The code provided by Carl Love is as follows:

1.       indets(eq[1], assignable(name));

or, doing all equations at once,

2.       indets({entries(eq, nolist)}, assignable(name));

 

Further Explaining the indets and type commands.

Both dharr and Carl Love provided an answer that used the indets command. In essence the indets command used in this example contains two arguments: indets(expr, typename). Expr is a rational expression that only uses the operations such as addition, subtraction, division, and multiplication. Typename is a parameter used when the desired return is a set containing all subexpressions in expr that are of type typename.

Carl Love used the assignable(name) argument  for the typename parameter in order to return all the variables that can be assigned a value, excluding constants such as Pi that are also considered names. Indeed, assignable is a type and can be used without an additional argument. For example, the command indets(x+f(x)+y=1, assignable) returns {x,y,f(x)} because all three symbols can be assigned values. However, indets(x+f(x)+y=1, assignable(name)) returns just {x,y} because f(x) is of type function, not of type name. Similarly, indets(x+y=Pi, assignable) would return just {x,y} because Pi is not considered to be something that can be assigned to.

Carl’s second command used ({entries(eq, nolist)} as the expr parameter. In this version, eq is the table whose members are the individual equations. Remember, the syntax x[1] creates a table whose name is x, and whose entry is the object assigned to x[1]. The entries(t) function returns a sequence of the table members, each member being placed in list brackets. By including the option nolist, the return is then a sequence of table members without list brackets. 

Finally, note that different programmers will use different approaches to finding “indeterminants” in expressions. Dr. Lopez pointed out that some years ago he asked three different programmers about extracting the “assignable names” in an expression such as q:=x+Pi+cos(a). The naive indets(q) returns {a,x,cos(a)}, whereas indets(q,name) returns {Pi,a,x}. However, select(type,indets(q),name) returns {a,x}, as does indets(q,And(symbol,Not(constant))).

Don Carota’s question is able to showcase some of the different types that are within Maple’s platform. Therefore, it is important to go over what the type-checking function is and what it does. In many contexts, it is not necessary to know the exact value of an expression; instead it is enough to know if the value belongs to a group of expressions that have similarities. Such groups are knows as types.

Maple’s engine uses the type function in every single procedure to direct and maintain the flow of control in algorithms and to decide if the user’s input is valid. There are about 240 different types that Maple recognizes including: prime, string, symbol, list, etc.  Let’s see some examples of how the function works using elements from this question. 

Type has two parameters: an expression e, and a valid type expression t. To check that the output of the entries(eq,nolist) is indeed not a list, the type command can be used as follows:

As expected, the last command returns false! If you want to learn more about the type and indets commands you can visit their corresponding help pages: ?type, ?indets.

 

This blog was written by Maplesoft’s intern Pia under the supervision of Dr. Robert Lopez. We both hope that you find this useful. If there is a particular question on MaplePrimes that you would like further explained, please let us know. 

My desk was covered with papers, a glass of water, and a big shipping container. Even though my chair was there, I was sitting on the floor with my laptop, having a bad hair day, and a robot was seated next to me.  This was a typical day at Maplesoft for an engineering co-op student.

For this project, at the request of my manager, I left my duties as Spanish translator and marketing assistant and I started to work with the robot NAO from Aldebaran Robotics. The purpose of this project was to program NAO using Aldebaran’s Choreographe software to make new movements and dances that I would later use to create new MapleSim models for Maplesoft’s Model Gallery. Maplesoft’s marketing team would then use these models in some of their promotional activities.

Given that NAO was going to travel to Taiwan in a short period of time, I wanted to focus on doing one elaborate dance and a couple of simple movements.Thanks to F.U.N. lab from the University of Notre Dame, I was able to focus on the detailed dance because they had an amazing Choreographe database of behaviour/movement code.   

I started this project with zero knowledge about Choreographe, but with a good understanding of NAO´s MapleSim model that the Maplesoft engineers had previously created. After a few weeks with NAO and some YouTube tutorials, I discovered that programming NAO was really easy. I would move NAO’s joints to the positions I wanted to, and then I would tap its head to record and save them. I did this for a couple of weeks making sure that the sequence of movements wouldn’t make NAO fall or break a finger. At this point I was already a NAO expert.

After finishing up all the movements and dances it was time to move on to the next phase of the project: obtaining the data for the MapleSim model. The MapleSim model was created using the Denavit-Hartenberg (DH) convention; therefore, I needed the values of the degrees of rotation of each joint while the robot performed a dance. These numbers were easily obtained using the “record” button in Choreographe and exporting them into a CSV file. This file was later attached to the MapleSim model, so it could be used in a time look up table. The input of NAO´s joints were then specified by using the values within this table.

I started by recording the simplest movements: NAO blowing kisses and doing the sprinkler. These were the best ones to start working on because in these examples, the robot only needs to move its upper body, meaning that the lower body didn’t need any flexibility. This gave me and Abtin Athari, Application Engineer at Maplesoft, the freedom to simplify the original model by removing unnecessary degrees of freedom on the lower body. Abtin and I also realized that at the beginning of some of the new movements the robot would have too much torque, so we extended some of the recorded position of the rotational joints so the robot could stay in the same position for a longer time. These modifications ensured that the model wouldn´t have any problems during any of the simulations.

To finish the project, I worked with the Marketing team to create some videos where we could display the real robot next to the MapleSim model doing the same movements. The purpose of these videos was to showcase the essence of the high-fidelity models that MapleSim allowed us to create. It was amazing to see how the MapleSim model corresponded so closely to the physical robot.

After three weeks of intense work and meetings, my days as a robot whisperer ended. I learned new things about robots, how to build models with MapleSim, and the processes behind developing videos. It was a project that allowed me to wear both an engineer’s and a marketer’s shoes.  I was able to put into practice my technical knowledge and problem solving skills; and at the same time I was able to enhance my creative and analytical skills by evaluating the quality and impact of my work.

Page 1 of 1