## 102 Reputation

8 years, 182 days

## How do I create a product of list elemen...

Maple

Hello,

I want to create an product expression from all list elements,

e.g.

expr := createproduct([a,b,c,d,1]);

expr:= a*b*c*d

Context; I'd like to apply a function on all factos of a polynomial. I can easily map(fun,factors(apolynomial)) which will output a list. However I do not know how to get back.

## How to optimize with an interpolated fun...

Maple 2018

Hi,

I want to optimize a function f(g(e(x),x))  where g(x) is interpolated and e(x) is not known when the interpolation object is created. I found the intpolation Methods of Maple 2018 very helpful to quickly change the inpterpolation method.

Unfotunately the optimization fails when the interpolation object receives an expression as input.

It works if I just use x. Do you know my the first approach fails?

Is there maybe a way to convert the interpolation object to a piecewise function?

 > # Optimize with Interpolation Object
 > points :=  <<0.4000|  10.0000>,             <0.7000|   10.0000>,             <1.0000|   10.0000>,             <0.3000|   30.0000>,             <0.4000|   30.0000>,             <0.5000|   30.0000>>; Data := <0617,0767,0220,0444,0692,0789>*0.001; intmethod := LinearInterpolation; g := Interpolation[intmethod](points(1..-1,1),Data):
 (1)
 > plot(g(x),x=0..1)
 > e := x/sqrt(2);
 (2)
 > f := 5+g(e)*x^2
 (3)
 > f_simple := 5+g(x)*x^2
 (4)
 > op_f := Optimization[Minimize](f(x),x=0 .. 1);
 > op_f_simple := Optimization[Minimize](f_simple(x),x=0 .. 1);
 (5)
 >

## Remove Assumptions for Code Generation...

Maple

Hi,

this has been asked before but none of the other answers worked for me. I want to generate code from expressions that were simplified using some assumptions. I the assumptions remain on the variables Maple replaces variable names wich I don't understand.

Here is my script where I tried to remove the assumptions based on the answers of previous questions. Unfortunately, even though assumptions are removed in the end, Code generation still replaces the variables.

Here is my minimal exaple Code:

 >
 > restart:
 >
 > # test removing assumptions
 > funa:=x+y
 (1)
 > Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));
 (2)
 > # make some assumptions
 > assume(x,real);assume(y,real);
 > x,y
 (3)
 > #place variables in a list
 > alist:=[x,y];
 (4)
 > # make cool interferences with assumptions...
 > # clear assumptions
 > # try 1 from https://www.mapleprimes.com/questions/207601-Remove-Assumptions-
 > nms:=convert(indets(alist,name),list); nmsS:=convert~(nms,string); L:=StringTools:-Substitute~(nmsS,"~",""); #Removing "~" L1:=parse~(L); S:=nms=~L1; Expr:=subs(S,expr);
 (5)
 > hasassumptions(x)
 (6)
 > # try 2
 > nops(alist)
 (7)
 > for i from 1 to nops(alist) do parse(cat(StringTools[Substitute]~(alist[i],"~",""),":='",StringTools[Substitute]~(alist[i],"~",""),"'")); end do;
 (8)
 > # check if assumtions are there
 > hasassumptions(x)
 (9)
 > #try 3 https://www.mapleprimes.com/questions/39555-Unassume
 > for u in alist do `property/object`[u]:=evaln(`property/object`[u]); `property/OrigName`[u]:=evaln(`property/OrigName`[u]); end do;
 (10)
 > hasassumptions(x)
 (11)
 > hasassumptions(y)
 (12)
 > alist
 (13)
 > funb:=x+y
 (14)
 > Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([eval(alist,1)])));
 (15)
 > Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));
 (16)
 >
 > CodeGeneration[C](Maple_proc, optimize, declare=[x::float,y::float], defaulttype=float, deducetypes=false, coercetypes=false, output="fun.c");
 >
 >
 >
 >
 >
 >
 >
 >
 >

## How to handle piecewise defined matrix v...

Maple

Hi,

I have some matrix valued functions that that I can only define piecewise. Unfortunately I have some problems to manipulate those functions. Piecewise sees to do the job for  scalar valued functions. There I can easily differentiale and add results. For Matrix valued functions however, those features seem to not be availible.

I can work around these limitations by manually applying all operations to the operands of the piecewise function. Like I show here:

 > restart:
 > # a piecewise function
 > p1:=piecewise(a(t)^2=0,,<1,1/a(t)>)
 (1)
 > # differentiation does not work
 > diff(p1,t)
 (2)
 > # selecting individual entries does not work
 > r:=p1(1)+1
 (3)
 > # strange simplification behaviour
 > simplify(p1)
 (4)
 > # current workaround, define new piecewise function by using op
 > r:=piecewise(op(1,p1),op(2,p1)(1)+1,op(3,p1)(1)+1)
 (5)
 >

I think my workaround is very cumbersome and error prone. Is there a better way to tackle this probem? Or should I try to overload all needed operations, and is this even possible?

Thanks for all suggestions!

Honigmelone

## Problem with codegen applied to a rectan...

Maple 2017

Hey,

I am using codegen to generate code from Maple expressions. More specifically I want to export non quadratic matix. The export works for matrices that have more columns than rows, but not for matrices with more rows than columns. There I receive the error:

Error, (in codegen/array/entry) 2nd index, 3, larger than upper array bound 2

A workaround would be to transpose the matrix before the export, and to transpose on the import (in Matlab) but I don't want to overcomplicate my code. Thanks for your help!

I have attached a demonstration worksheet below.

 > ## test codegen non quadratic returnarray
 > ## works #Myfun := Matrix(3,3)
 > ## works #Myfun := Matrix(2,3):Myfun(2,3):= 3*b;
 > ## does not work Myfun := Matrix(3,2):Myfun(3,2):= 3*b;
 (1)
 > Myfun(1):= 7*a;
 (2)
 > ## codegen works best with arraybased matrices, and call by reference becomes possible
 > returnArray:= convert(Myfun,matrix)
 (3)
 > codegen[makeproc](Myfun,[a,b])
 > codegen[makeproc](returnArray,[returnArray,a,b])
 > codegen[makeproc](returnArray,[a,b])
 >