Why cases not works in math expression block

yugang
yugang Altair Community Member
edited October 2020 in Community Q&A

Hello, I try to creat a math expression block, with cases function.

I follow excately the example in extend defintion document.

But I don't understand why it not works.

For example, there is a sin waveform generator. Output 1 when signal >0, output -1 when signal <0.

Can anyone takes a look and help.

Thanks.

 

Unable to find an attachment - read this blog

Answers

  • RoKet
    RoKet
    Altair Employee
    edited April 2020

    Hi Yugang,

    As I understand it, switch case expressions are not allowed in math expression block (see help). An alternative is the use of OML-block.

    Furthermore switch/case has to be used in another way: switch gets an expression, case the result. In your case if/else is the right construct. I've attached an example.

    Unable to find an attachment - read this blog

  • Livio Mariano_20459
    Livio Mariano_20459
    Altair Employee
    edited April 2020

    RoKet is right switch/case is not allowed but...

    You can get exactly what you need with MathExpression block in a much easier way. Please look the attached model.

    Trick:

    - You set your condition between round brackets, if condition is true, it is like having 1 otherwise 0.

    You can use & symbol for 'and' and | symbol for 'or' in the formulation of you condition.

    -  You multiply the condition by the associated operation that you want to perform when the condition is true (=1).

    - When the condition is false it will return 0 and you will not consider the operation associated to it.

    - You sum the other conditions multiplied by the associated operations.

     

    This way of using the MathExpression block is really powerful and flexible, I use it a lot.   

    Unable to find an attachment - read this blog

  • yugang
    yugang Altair Community Member
    edited April 2020

    Thanks for the answers from RoKet and Livio Mariano.

    I think I make mistake between math expression and matrix expression.

    The if and switch works well in matrix expression not math.

    Thanks for your answer, I think RoKet's solution is normal one.

    And the one from Livio Mariano is very tricky and interesting.

    Have a nice day.

    :D/emoticons/default_biggrin.png' srcset='/emoticons/biggrin@2x.png 2x' title=':D' width='20' />

  • João Marabisa
    João Marabisa
    Altair Employee
    edited July 2020

    Both suggestions are really helpful, thank you guys...

     

    I was having a problem using 'If Expressions (Pallete - ActivationOperations)' because my output should be a scalar and not a Matrix. And using that option I would have different output ports, and it does not work for my case.

    I've decided to try Livio's suggestion (as a challenge) and it worked. Besides tricky, is pretty elegant. :D/emoticons/default_biggrin.png' srcset='/emoticons/biggrin@2x.png 2x' title=':D' width='20' />

     

    To illustrate you a case, I have 1 input (Td) and all the other variables are set by the user, and it has to respect the following equations:

     

    act.png.56d7a721004cea28e584ad77079e4d36.png

     

    I had exactly the result that I was expecting! I've attached the file for those who might be interested.

     

    Cheers!

    Unable to find an attachment - read this blog

  • Fady Nassif_21652
    Fady Nassif_21652
    Altair Employee
    edited July 2020

    Hi Yugang,

    As you have seen, you have several solution.

    Note that for performance reason, it is sometimes better to use the MathExpression or MatrixExpression instead of OmlCustomBlock.

    The OmlCustomBlock code is executed in the interpreter, the Mathexpression code is converted into c code so it runs faster.

     

    Thanks

    Fady