Honigmelone

70 Reputation

One Badge

3 years, 64 days

MaplePrimes Activity


These are questions asked by Honigmelone

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

 

Hi,

I want to apply a rule to simplify an expression. The applyrule command works, when used directly in the worksheet. When I try to use the command within a procedure, Maple throws an error I cannot decipher:

Error, (in PatternMatching:-AlgStruct:-InsertPattern) first operand of `::' must be a name

 

Here is a full demonstration worksheet:

restart:

anexp:=abs(x)^2;

abs(x)^2

(1)

simplify(anexp);

abs(x)^2

(2)

rmabssq := proc(inexp)
description "removes the abs^2 construct in an expression":
local ruleabssqared1,ruleabssqared2,outexp:
    ruleabssqared1:= abs(''a''::algebraic)^2= ''a''^2:
    ruleabssqared2:= abs('expand'(-''a'')::algebraic)^2= ''a'':
    outexp:= applyrule([ruleabssqared1,ruleabssqared2],inexp):
    return outexp:
end proc;
 

proc (inexp) local ruleabssqared1, ruleabssqared2, outexp; description "removes the abs^2 construct in an expression"; ruleabssqared1 := abs(''a''::algebraic)^2 = ''a''^2; ruleabssqared2 := abs((('expand')(-''a''))::algebraic)^2 = ''a''; outexp := applyrule([ruleabssqared1, ruleabssqared2], inexp); return outexp end proc

(3)

## does not work :(
rmabssq(anexp);

Error, (in PatternMatching:-AlgStruct:-InsertPattern) first operand of `::' must be a name

 

## works!
ruleabssqared1:= abs(''a''::algebraic)^2= ''a''^2:
ruleabssqared2:= abs('expand'(-''a'')::algebraic)^2= ''a'':
newexp:= applyrule([ruleabssqared1,ruleabssqared2],anexp);

x^2

(4)

 


 

Download applyrule_4.mw

Thanks for your help

Hey,

I want to solve this equation and looking at the plot there are at least 3 solutions. I want the greatest/smallest negative solution. Unfortunately using solve with assumptions produces no results and solve without assumptions only finds two solutions.

Can you please help me?

#select greatest negative value from solution

restart:

expr:= ax*cos(lambda)+ay*sin(lambda)-(a+b*lambda)

ax*cos(lambda)+ay*sin(lambda)-b*lambda-a

(1)

ax:=1:ay:=2:a:=0.5:b:=0.25: #examplanatory values

plot(expr)

 

 

assume(-2*Pi<lambda,lambda<0): #does not work

 

sol_lambda:=[solve(expr=0,lambda, useassumptions)];# returns empty list even though without assumption one solution is found

Warning, solutions may have been lost

 

[]

(2)

sol_lambda:=[solve(expr=0,lambda)]; #returns only two solutions even though looking at the plot 3 are there

Warning, solve may be ignoring assumptions on the input variables.

 

Warning, solutions may have been lost

 

[2.190357220, -.2688724573]

(3)

sol_l_v:=evalb~(sol_lambda<~0); #dirty workaraound

[false, true]

(4)

sol_l_add:=[ListTools:-SearchAll(true,sol_l_v)] ; #this seems overly complicated

 

[2]

(5)

lambda:=sol_lambda[sol_l_add[-1]];  #to select the last entry

 

-.2688724573

(6)

expr; #test

 

0.

(7)

 


Download select_solution.mw

Thanks!

Honigmelone

1 2 3 Page 1 of 3