Group Abstract Group Abstract

Message Boards Message Boards

MatlabForm and ComsolForm

I extensively use Comsol for my work and I use Mathematica to modify or create some expressions that I utilize in Comsol.

Comsol has the same syntax as Matlab (for the most part). Hence, I created a simple function that outputs an expression ready to be inputted in Comsol.

I use the fact that FortranForm is pretty similar to Matlab, hence:

MatlabForm[L_List] := MatlabForm/@L
MatlabForm[expr_] := ToString@FortranForm[expr /. {
  Complex[0, Rational[1, b_]] :> "1j"/MatlabForm@b,
  Complex[0, Rational[a_, b_]] :> "1j"*MatlabForm[MatlabForm@a/MatlabForm@b],
  Complex[0, b_] :> "1j"*MatlabForm@b,
  Complex[a_, b_] :> MatlabForm@a + "1j"*MatlabForm@b,
  a_^Rational[1,2] | Sqrt[a_] :> sqrt@MatlabForm@a,
  E^x_ :> exp@MatlabForm@x,
  Rational[1, b_] :> 1/MatlabForm@b,
  Rational[a_, b_] :> MatlabForm[MatlabForm@a/MatlabForm@b],
  (op:Abs|Cos|Sin|Tan|Exp)[x_] :> ToLowerCase[ToString@op][MatlabForm@x],
  Conjugate[x_] :> "conj"@MatlabForm@x,
  Re[x_] :> "real"@MatlabForm@x,
  Im[x_] :> "imag"@MatlabForm@x
}] // StringReplace[#, {"**" -> "^", "\"" -> ""}] &

Which basically deals with some very basic (but nonetheless useful) operations/functions.

The final result also needs to human readable for debug purposes, so I created another function:

MatlabFormSimplify[expr_] := Block[{res},
    ClearAttributes[{Plus, Times}, Orderless];
    res = MatlabForm@Simplify[Simplify@expr, ComplexityFunction -> StringLength@*MatlabForm,
     TransformationFunctions -> {
       Automatic,
       Function[{e}, e /. (op:Times|Plus)[a___, b_, c_, d___] :> op[a, c, b, d]]
     }];
    SetAttributes[{Plus, Times}, Orderless];
    res
]

Which strip-out the Orderless attribute of Plus and Times, so b-a never evaluates to -a+b, which is ugly and has too many symbols.

A real example would be:

Cross[{Ax, Ay, Az}, Conjugate@{Bx, By, Bz}] // MatlabFormSimplify // TableForm
(* output *) 
"conj(Bz)*Ay - conj(By)*Az"
"conj(Bx)*Az - conj(Bz)*Ax"
"conj(By)*Ax - conj(Bx)*Ay"

Compare with the standard Mathematica output:

{-Az Conjugate[By] + Ay Conjugate[Bz], Az Conjugate[Bx] - Ax Conjugate[Bz], -Ay Conjugate[Bx] + Ax Conjugate[By]}

PS: I don't usually output lists as Matlab [...], since, as I already said, I use Comsol, hence is more important to define scalar quantities based on symbolic expressions. PSS: More functions can be easily added. I rather created my own function, that I fine-tuned, than use an outdated Package.

POSTED BY: Thales Fernandes
10 Replies
Posted 5 years ago
POSTED BY: Baker Ren
POSTED BY: Thales Fernandes
POSTED BY: Szabolcs Horvát
POSTED BY: Thales Fernandes

That is literally the case in MATLAB. MATLAB does not know about scalars or vectors. The lowest-dimensional array it can work with is 2, even for types like struct or char (!!) It is called MATrix LABoratory for a reason.

A 1x1 matrix is what we usually call scalars and 1xn or nx1 matrix is what we call vectors... In Matlab you can totally fine run something like:

x = 2;
y = x^2;
z = y.^2;

Whereas y and z give the same result. But if the definition of x is changed to a vector or non-square matrix we get for y:

error

So, obviously, Matlab knows what a scalar is... It is a 1x1 matrix!

So the drawback of using such a package is this one, it always assumes the variables to be matrices (or tensors). And, as stated, I mainly use this piece of code in Comsol, but, it could be used entirely fine in Matlab.

POSTED BY: Thales Fernandes

But an 18-year-old package seems quite a lot to me.

Old is not usually bad :-) It is designed to be easily extensible.

It was also made with the thought that everything is a matrix.

That is literally the case in MATLAB. MATLAB does not know about scalars or vectors. The lowest-dimensional array it can work with is 2, even for types like struct or char (!!) It is called MATrix LABoratory for a reason.

Comsol does not accept such a things.

Of course, then you need something else.

POSTED BY: Szabolcs Horvát

I extensively use Comsol for my work and I use Mathematica to modify or create some expressions that I utilize in COMSOL.

For 'modify' part, don't you also need an inverse definitions to import an existing COMSOL expression into Mathematica for simplification?

POSTED BY: Kapio Letto

Have you seen the ToMatlab package? It seems like a natural foundation to build on top of.

Yes, I have. But an 18-year-old package seems quite a lot to me. It was also made with the thought that everything is a matrix. So every expression has a "." preceding it. Comsol does not accept such a things. I needed something simpler. No need to get overly complicated with things, the bare bones would suffice. And what better ways do to things as to "recycle" a built-in function to do your deeds?

POSTED BY: Thales Fernandes

Have you seen the ToMatlab package? It seems like a natural foundation to build on top of.

POSTED BY: Szabolcs Horvát
Posted 9 years ago

I am also a COMSOL user. Thanks very much for this!

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard