Honigmelone

102 Reputation

8 Badges

8 years, 217 days

MaplePrimes Activity


These are questions asked by Honigmelone

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.

Thanks for your comments!

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?

 

Thank you for your help
 

# 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):

Matrix(6, 2, {(1, 1) = .4000, (1, 2) = 10.0000, (2, 1) = .7000, (2, 2) = 10.0000, (3, 1) = 1.0000, (3, 2) = 10.0000, (4, 1) = .3000, (4, 2) = 30.0000, (5, 1) = .4000, (5, 2) = 30.0000, (6, 1) = .5000, (6, 2) = 30.0000})

 

Vector[column](%id = 18446884324022199230)

 

LinearInterpolation

(1)

plot(g(x),x=0..1)

 

e := x/sqrt(2);

(1/2)*x*2^(1/2)

(2)

f := 5+g(e)*x^2

"5+(module() ... end module)(module() ... end module,1/2 x sqrt(2)) x^2"

(3)

f_simple := 5+g(x)*x^2

"f_simple:=5+[[["a linear interpolation object"],["with 6 points in 1-D"]]](x) x^2"

(4)

op_f := Optimization[Minimize](f(x),x=0 .. 1);

Error, (in Optimization:-NLPSolve) non-numeric result encountered

 

op_f_simple := Optimization[Minimize](f_simple(x),x=0 .. 1);

[HFloat(4.999979117244145), [x = HFloat(0.028901647183245588)]]

(5)

 

NULL


 

Download convert_interpolation_object.mw

 

 

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.

Thank you in advance!

Here is my minimal exaple Code:

 

restart:

 

# test removing assumptions

funa:=x+y

x+y

(1)

 Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));

proc (alist) x+y end proc

(2)

# make some assumptions

assume(x,real);assume(y,real);

x,y

x, y

(3)

#place variables in a list

alist:=[x,y];

[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);

[x, y]

 

["x~", "y~"]

 

["x", "y"]

 

[x, y]

 

[x = x, y = y]

 

expr

(5)

hasassumptions(x)

true

(6)

# try 2

nops(alist)

2

(7)

for i from 1 to nops(alist) do
parse(cat(StringTools[Substitute]~(alist[i],"~",""),":='",StringTools[Substitute]~(alist[i],"~",""),"'"));
end do;

'x'

 

'y'

(8)

# check if assumtions are there

hasassumptions(x)

true

(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;

`property/object`[x]

 

`property/OrigName`[x]

 

`property/object`[y]

 

`property/OrigName`[y]

(10)

hasassumptions(x)

false

(11)

hasassumptions(y)

false

(12)

alist

[x, y]

(13)

funb:=x+y

x+y

(14)

 Maple_proc := codegen[makeproc](funb,(ListTools[Flatten]([eval(alist,1)])));

proc (x, y) x+y end proc

(15)

 Maple_proc := codegen[makeproc](funa,(ListTools[Flatten]([eval(alist,1)])));

proc (x, y) x+y end proc

(16)

 

 CodeGeneration[C](Maple_proc, optimize, declare=[x::float,y::float], defaulttype=float, deducetypes=false, coercetypes=false, output="fun.c");

Warning, the following variable name replacements were made: x~ -> cg, y~ -> cg1

 

 

>

 

 

 

 

 

 

 

 


 

Download testunassuming.mw

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,<cos(a(t))^2+sin(a(t))^2,0>,<1,1/a(t)>)

p1 := piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)}))

(1)

# differentiation does not work

diff(p1,t)

diff(piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})), t)

(2)

# selecting individual entries does not work

r:=p1(1)+1

r := (piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})))(1)+1

(3)

# strange simplification behaviour

simplify(p1)

piecewise(t = RootOf(a(_Z)), _z1(RootOf(a(_Z))), Vector(2, {(1) = 1, (2) = 1/a(t)}))

(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)

r := piecewise(a(t)^2 = 0, cos(a(t))^2+sin(a(t))^2+1, 2)

(5)

 


 

Download Scratch.mw

 

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

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;

Myfun := Matrix(3, 2, {(1, 1) = 0, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0, (3, 1) = 0, (3, 2) = 3*b})

(1)

Myfun(1):= 7*a;

Myfun := Matrix(3, 2, {(1, 1) = 7*a, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0, (3, 1) = 0, (3, 2) = 3*b})

(2)

## codegen works best with arraybased matrices, and call by reference becomes possible

returnArray:= convert(Myfun,matrix)

returnArray := Matrix(3, 2, {(1, 1) = 7*a, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0, (3, 1) = 0, (3, 2) = 3*b})

(3)

codegen[makeproc](Myfun,[a,b])

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

 

codegen[makeproc](returnArray,[returnArray,a,b])

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

 

codegen[makeproc](returnArray,[a,b])

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

 

 


 

Download codegen_rectangular_matrix.mw

 

1 2 3 Page 1 of 3