Some of the following functions are covered in Mathematica:
RealDigits
Representation of decimals in the form of individual digits and their decimal exponent (but missing sign bits)
FromDigits
RealDigits
restores this number from the result, but is expressed as a rational number form (unable to recover missing symbol bit information)
Sign
Taking into account the loss of symbols, you need to get the symbols through the Sign
function and multiply them.
These are mainly for real operations, when dealing with complex numbers, it is necessary to use Im
and Re
function to extract the imaginary part and the real part after processing, and then multiply the real and imaginary units by the imaginary unit to add the converted and restored complex number.
For vector and matrix operations, it is possible to use Map
Apply
commands (functions) such as.
An example might be more vivid and operable. For example, this mat binary data format file (if the link fails, the user can use the random number generator to generate some data as a practice), including x The complex matrix A And x1 The real vector b 。
How do you turn them into the form of a rational number representation?
Import
Import Data First:
data=Import["e:\\Downloads\\data1.mat"];
The resulting data
one contains two elements List
:
Complex matrices a 18 x 18 Placed indata[[1]]
, while the real vector b 18 x 1 Placed indata[[2]]
In The code to convert them is:
(Map[FromDigits, [email protected]([email protected][[1]]), {2}]* Sign[data[[1]] // Re] + I *Sign[data[[1]] // Im]* Map[FromDigits, [email protected]([email protected][[1]]), {2}])
And
(FromDigits @@@ RealDigits[data[[2]]])*Sign[data[[2]]]
As can be seen from the above, the conversion of vectors is simple, and the conversion of real numbers is especially. However, it is quite cumbersome to convert matrices, especially complex matrices. Fortunately original aim. After a little adjustment, this completes the conversion.
The pictures below don't know what's going on??
Converting double-precision complex numbers to real and imaginary parts in the form of rational numbers