Art Kalb

217 Reputation

11 Badges

16 years, 32 days

MaplePrimes Activity


These are questions asked by Art Kalb

I have been writing a language translator. I have everything working reasonably well except that when translating abs(expr) with optimize=tryhard, the IntermediateCode converts the abs() into if...then...else statements. This would be fine except the type of the expr is generally a complex number. I need to optimize the code because there is a lot of redundant calculations otherwise.

I can't figure out how to get IntermediateCode from breaking the abs function into if...then...else statements. [My solution so far is to substitute abs with a dummy name and then use the translator to translate the dummy name into an abs statement - That's really not how things should work!]

Any suggestions?

I've included some example test cases of what is going wrong.

with(CodeGeneration)

testproc1 := proc (x) abs(x) end proc

IntermediateCode(testproc1, optimize = tryhard)

Scope( nametab,
  AssignedName(Name("testproc1"), Scope( nametab,
    Procedure(
      ParameterSequence(Declaration(Name("x"), Type(integer))),
      LocalSequence(Declaration(Name("s1"), Type(integer))),
      OptionSequence(),
      ExpressionSequence(),
      StatementSequence(
        If(
          ConditionalPair(LessEqual(Integer(0), Name("x")), StatementSequence(
            Assignment(Name("s1"), Name("x"))
          )),
          ConditionalPair(Less(Name("x"), Integer(0)), StatementSequence(
            Assignment(Name("s1"), Negation(Name("x")))
          )),
          StatementSequence(
            Assignment(Name("s1"), Integer(0))
          )
        ),
        Return(Name("s1"))
      ),
      DescriptionSequence(),
      GlobalSequence(),
      LexicalSequence(),
      Type(integer)
    )
  ))
)

 

testproc2 := proc (x::numeric) abs(x) end proc

IntermediateCode(testproc2, optimize = tryhard)

Scope( nametab,
  AssignedName(Name("testproc2"), Scope( nametab,
    Procedure(
      ParameterSequence(Declaration(Name("x"), Type(numeric))),
      LocalSequence(Declaration(Name("s1"), Type(numeric))),
      OptionSequence(),
      ExpressionSequence(),
      StatementSequence(
        If(
          ConditionalPair(LessEqual(Float(0, 0), Name("x")), StatementSequence(
            Assignment(Name("s1"), Name("x"))
          )),
          ConditionalPair(Less(Name("x"), Float(0, 0)), StatementSequence(
            Assignment(Name("s1"), Negation(Name("x")))
          )),
          StatementSequence(
            Assignment(Name("s1"), Float(0, 0))
          )
        ),
        Return(Name("s1"))
      ),
      DescriptionSequence(),
      GlobalSequence(),
      LexicalSequence(),
      Type(numeric)
    )
  ))
)

 

NULL

testproc3 := proc (x::complex) abs(x) end proc

IntermediateCode(testproc3, optimize = tryhard)

Scope( nametab,
  AssignedName(Name("testproc3"), Scope( nametab,
    Procedure(
      ParameterSequence(Declaration(Name("x"), Type(complex))),
      LocalSequence(),
      OptionSequence(),
      ExpressionSequence(),
      StatementSequence(
        Return(FunctionCall(Name("abs"), ExpressionSequence(Name("x")), unknown))
      ),
      DescriptionSequence(),
      GlobalSequence(),
      LexicalSequence(),
      Type(numeric)
    )
  ))
)

 

NULL

Download intermediate_code_abs.mw

Hi,

I'm trying to figure out how to get the HermitianTranpose vector to distribute over a sum of vectors. Maple seems to want to ignore the linearity of the conjugate operation.

See the example code attached.

Thanks.
 

alp := [a, b, c, d, e]

[a, b, c, d, e]

(1)

u[a] := Vector(5, proc (m) options operator, arrow; u[alp[m], a] end proc)

u[b] := Vector(5, proc (m) options operator, arrow; u[alp[m], b] end proc)

u[c] := Vector(5, proc (m) options operator, arrow; u[alp[m], c] end proc)

u[d] := Vector(5, proc (m) options operator, arrow; u[alp[m], d] end proc)

u[e] := Vector(5, proc (m) options operator, arrow; u[alp[m], e] end proc)

NULL

NULL

NULL

N := r[a]*u[a]+r[b]*u[b]+r[c]*u[c]+r[d]*u[d]+r[e]*u[e]

Vector[column](%id = 36893490335086041316)

(2)

``

LinearAlgebra:-HermitianTranspose(N)

Vector[row](%id = 36893490335105454316)

(3)

NULL


 

Download DistributeHermitian.mw

By default, CodeGeneration generates temporary names during optimize of t####. For compatibility with other code, I would like to have flexibility in choosing the preamble to the temporary variable name ("t" is default). Is there a way to do this?

I don't see anything in the documentation, but it seems like something one should be able to do.

(My target language is Python - although I'm not really using Python, the syntax is compatible).

I am trying to get Maple to recognize that

diff(x^n,x) does not equal n*x^(n-1), but rather 0 if n=0, or n*x^(n-1) otherwise.

This comes up when differentiating an infinite sum (power series). The constant term gets transformed into n/x instead of becoming zero. Maybe this is really a bug/lack of feature in how sum/Sum works.

For example: diff(Sum(x^n, n = 0 .. infinity), x) yields

 

Does anybody have a fix to get the differentiation right (other than expanding some terms of the series before taking the derivative)? 

(edited) Please note I am not trying to get an answer to this specific question. It is just illustrates a simplified example of behavior that leads to other bizarre results. Please see the attached worksheet for more weirdness: Problems_with_summation_Mapleprimes.mw

Hi,

I just upgraded to Maple 2021 and started getting strange results - similar calculations seemed to work in Maple 2020.

I am looking at correlated bivariate normal distributions. When doing a fully symbolic verification of the normalization, I get an incorrect result of infinity. I also get strange results when integrating over a circular region, but the normalization seemed to be a very elementary calculation.

Maybe someone can check this make sure I'm not losing my marbles. I've attached a worksheet.

Thanks.

Bivariate_Gaussian.mw

1 2 3 4 5 6 7 Page 1 of 7