Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.3K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Unexpected behavior of CoefficientArrays

Posted 3 years ago

See my following example:

In[778]:= gen="-y,-z,-x";
toCA[gen_]:=Module[{g,x,y,z},
g="{"<> gen <> "}"//ToExpression;
CoefficientArrays[g,{x,y,z}]//Normal
]

toCA[gen] 

"{"<> gen <> "}"//ToExpression;
CoefficientArrays[%,{x,y,z}]//Normal

Out[780]= {{-y, -z, -x}}

Out[782]= {{0, 0, 0}, {{0, -1, 0}, {0, 0, -1}, {-1, 0, 0}}}

Why does the same code return an incorrect result after being written as a function?

Regards, Zhao

POSTED BY: Hongyi Zhao
4 Replies
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Eric Rimbey
Posted 3 years ago

Thank you for pointing out this subtle difference. I tried the following method, but still failed.

In my situation, there are two wl script files opened in Mathematica. In one file, I created the following two functions and evaluated them:

toRep//ClearAll;
toRep[gen_?StringQ]:=Block[{coef,x,y,z}, 
coef=CoefficientArrays[gen,{x,y,z}]//Normal;
AffineTransform[{coef[[2]],coef[[1]]}]//TransformationMatrix
]
toReps//ClearAll;
toReps[gens_?StringQ]:=Block[{s,x,y,z}, 
s=StringSplit[gens,"\n"];
toRep/@("{"<>#<>"}"&/@s//ToExpression)
]

Then in another wl file, I evaluated the following code snippet:

In[42]:= (*
The Space Group has been identified as (No. )

Input generators
-y, -z, -x
y, -x, -z
1/8 + x, 1/8 + y, 1/8 + z

Transformation Matrix to the standard/default setting
(   
)
*)
SGGenSetWrongXYZ="-y,-z,-x
y,-x,-z
x+1/8,1/8+ y,z+1/8";

(*StringSplit[SGGenSetWrongXYZ,"\n"];
SGGenSetWrong=toRep/@("{"<>#<>"}"&/@%//ToExpression);*)
toReps[SGGenSetWrongXYZ]

Out[43]= {toRep[{-y, -z, -x}], toRep[{y, -x, -z}], 
 toRep[{1/8 + x, 1/8 + y, 1/8 + z}]}

As you can see, a portion of the result still appears as unevaluated.

Regards, Zhao

POSTED BY: Hongyi Zhao
Posted 3 years ago

Use Block instead of Module

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