Are there potential input errors in manually entered mathematical expressions?
Closed this issue · 3 comments
In this context, I believe that text exported using software like Mathematica may be more accurate, as the physical and mathematical formulas in many early documents were prone to printing errors.
Based on this, I tested a nb script, which can import the results of nb calculations and be called directly by matlab.
- nb code
- matlab load txt
- matlab using expressions
such as:
persistent expressions;
if isempty(expressions)
expressions = load_wigner_expressions('WignerD_Expressions.txt');
end
codes:
- nb code:
(Define the range of J values you are interested in)
Jvalues = {1/2, 1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6};
WignerDExpressions[J_] :=
Module[{alpha, beta, gamma, m1, m2, expressions, expr},
expressions = {};
Do[expr = WignerD[{J, m1, m2}, alpha, beta, gamma];
expressions =
Append[expressions, {J, m1, m2,
ToString[
FullSimplify[
expr /. {alpha -> [Alpha], beta -> [Beta],
gamma -> [Gamma]}, {Element[[Alpha], Reals],
Element[[Beta], Reals], Element[[Gamma], Reals]}],
FormatType -> StandardForm]}], {m1, -J, J, 1}, {m2, -J, J,
1}];
expressions]
(Generate expressions for all J values)
allExpressions =
Flatten[Table[WignerDExpressions[J], {J, Jvalues}], 1];
(Export the expressions to a file)
Export["WignerD_Expressions.txt", allExpressions, "Table"]
(Display the generated expressions)
allExpressions
- matlab call funs;
function expressions = load_wigner_expressions(filename)
% Load Wigner D-matrix expressions from a text file
%
% expressions = load_wigner_expressions(filename)
%
% Inputs:
% filename - Path to the text file containing expressions
%
% Outputs:
% expressions - Struct array with fields J, m1, m2, expression
fid = fopen(filename, 'r');
if fid == -1
error('Could not open file: %s', filename);
end
% First pass: count lines
num_lines = 0;
while ~feof(fid)
line = fgetl(fid);
if ischar(line)
num_lines = num_lines + 1;
end
end
fclose(fid);
% Preallocate struct array
expressions = struct('J', cell(1, num_lines), 'm1', cell(1, num_lines), 'm2', cell(1, num_lines), 'expression', cell(1, num_lines));
% Second pass: read lines and populate struct array
fid = fopen(filename, 'r');
index = 1;
while ~feof(fid)
line = fgetl(fid);
if ~ischar(line)
break;
end
% Parse the line assuming the format: J, m1, m2, expression
tokens = strsplit(line, ',');
if numel(tokens) == 4
expressions(index).J = str2double(tokens{1});
expressions(index).m1 = str2double(tokens{2});
expressions(index).m2 = str2double(tokens{3});
expressions(index).expression = strtrim(tokens{4});
index = index + 1;
end
end
fclose(fid);
end
test code;
Uploading test code.txt…
Thanks for looking into this.
Have a look at the test wignerd_singlevalues.
It numerically tests all explicit expressions hard-coded in wignerd/littled_explicit against values calculated using a matrix exponential with expm
.
The test passes, so the currently implemented explicit expressions are all correct.