Items tagged with map map Tagged Items Feed

I have a long list of two element lists, for example,

A := [ [2,3], [4,5] ,[6,7]];

I want apply '/' to the elements of each sublist.

The result will be

R := [2/3, 4/5, 6/7];

I do not need the list, I could use vectors or matrices.

Is it possible to do this other than by iteration?

Tom Dean

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. 

Assume we have a map f from a polynomial ring R to another polynomial ring S, I know how to compute kernel (a generator for the kernel ideal) of these maps by Singular, but I want to know can I do it with Maple too? Thanks.

An example;

Consider the homomorphism f:k[x,y]-->k[u,v] sending x to v and y to v^2 then using Singular;

ring r1=0,(x,y),lp;

ring r2=0,(u,v),lp;

ideal i=v,v2;

map f=r1,i;

setring r1;

kernel(r2,f);

_[1]=x2-y

So at above I took k a field of characteristic zero. The kernel is the ideal generated by x^2-y.

map(f, foldl(`*`, 1, a, b));
foldl(`*`, 1, map(f,a), map(f,b));

map(f, foldl(`+`, 0, a, b));
foldl(`+`, 0, map(f,a), map(f,b));

i discover it can auto calculate ring homomorphism, 

if i define a function f below

factor(map(f, foldl(`*`, 1, a, b))+map(f, foldl(`*`, 1, b, c))) = map(f, foldl(`*`, 1, a, c));

how to convert this f into a function with two input parameters?

 

Hello,

I have a maple code, which some expressions have more that 80000 terms and I need to double integrate them. The terms have sine, cossine trigonometric functions.

I tried to used de MAP command, it works for the first expressions but, after a while, Maple displays an error message related to too large expression.

Do you know how to handle large expressions ?

Thank you.

Hello,

I use a map() command to get the values of function for each element of vector. Here is an example of a simple task:

restart:

A:=[1,2,8];

f1:=x->2*x;
f1_table_A:=Vector([map(f1,A)]);

I would like to do the same with piecewise function but it doesn't work. Here is an example:

restart:
with(plots):
f:=2*x:
g:=x^2:

h:=x->piecewise( 0<=x and x<= 5, f, 5<x and x <= 10, g ) ;
A:=[1,2,8]:
h_table_A:=map(h,A):

Is it possible to work with piecewise function? Maybe someone has an idea how to do it in different way?

Kind regards,

Iza

Hi, 

What I want is quite simple but I have not seen it implemented anywhere yet. 

I have an explicit map F from the plane into itself. (not an ODE but simply a map) 

Then I have a set X, (something like a line segment or a rectangle in the plane) for which I 

really need to keep track of the images $F(X), F^2(X), F^3(X)$, and so on.

Is there a way to plot the image of a set in Maple? 

Thank you very much for your interests in the question. 

 

 

 

Is there a way to make the first part of this look like the second picture depicted here? Also after intigration is there a way to make the highlighted posrtion not have an "ln(e)" parts and just have the exponetials and there constants?

Obviously I dont want to have to manually input this section everytime, is there some command I can use to achieve this goal?

Above is the outcome I am getting at the current time... and I used map(expand,...) to do the expansion then simplifing it goes back to the origional. 

Below is the outcome I would like to achieve...you can see that this is a different method. The problem I have is this is the right click method of expansion and I am tring to build a program that solves matrix ODE's by just inputing the initial matrix and it doing everything so i can check my answers step by step. 

So my question is, is there another method of command input to get the bottom line in the below picture?

 

 

Page 1 of 1