typecast — Reinterpret numeric bytes as another class without converting values.
Y = typecast(X, newtype) preserves the native byte sequence of a full numeric or logical scalar or vector and decodes it as newtype. Y = typecast(X, "like", prototype) selects the host output class and complexity from a prototype.
Syntax
Y = typecast(X, newtype)
Y = typecast(X, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | NumericArray | Yes | — | Full numeric or logical scalar or vector. |
newtype | StringScalar | Yes | — | Numeric or logical output class. |
like | StringScalar | Yes | — | Literal string "like". |
prototype | LikePrototype | Yes | — | Host output class and complexity prototype. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Reinterpreted scalar or vector. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:typecast:InvalidArgument | The requested class, prototype, or argument list is invalid. | typecast: invalid argument |
RunMat:typecast:InvalidInput | The input is sparse, is not a scalar or vector, or has an incompatible byte count. | typecast: invalid input |
RunMat:typecast:GpuUnsupported | A GPU call uses complex or logical input or the like syntax. | typecast: unsupported gpuArray form |
RunMat:typecast:Internal | Exact gather, reconstruction, or resident restoration fails. | typecast: internal error |
How typecast works
- The input must be a full scalar or vector. Sparse arrays and nonscalar matrices are rejected.
- The input byte count must be divisible by the requested output element width.
- Row inputs produce rows and column inputs produce columns; changing element width changes the element count without changing the byte sequence.
- Double, single, logical, and all eight integer classes use their authoritative native storage and the platform's native byte order.
- The like form can produce real or complex host output and groups adjacent decoded values into real and imaginary components for a complex prototype.
- Character input and output are not currently available because RunMat's character value representation cannot preserve every raw UTF-16 code unit.
- Real numeric gpuArray input preserves exact bytes, output class, shape orientation, and the owning provider. Unsupported GPU forms fail terminally rather than gathering through the general dispatcher.
Examples
Inspect the native bytes of unsigned integers
bytes = typecast(uint32([1 256]), "uint8")Round-trip wide integer bit patterns exactly
values = uint64([9007199254740993 intmax("uint64")]);
copy = typecast(typecast(values, "uint8"), "uint64")Select paired complex integer output with a prototype
z = typecast(int16([-1 2 -3 4]), "like", complex(int16(0)))Using typecast with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how typecast changes the result.
Run a small typecast example, explain the result, then change one input and compare the output.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · uint16 · uint32 · uint8
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how typecast is executed, line by line, in Rust.
- View the source for typecast in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.