What does the ctranspose function do in MATLAB / RunMat?
B = ctranspose(A) (or B = A') flips the first two dimensions of A and conjugates
complex values. Real-valued inputs therefore behave like transpose, while complex inputs
receive Hermitian conjugation.
How does the ctranspose function behave in MATLAB / RunMat?
- Works for scalars, vectors, matrices, and N-D arrays; only the first two axes are swapped.
- Real numeric, logical, and character data are not changed by conjugation.
- Complex values receive element-wise conjugation after the transpose (
(a + bi)' = a - bi). - Character arrays and cell arrays preserve their types;
ctransposesimply rearranges entries. - String scalars are passed through unchanged; string arrays transpose like MATLAB.
- Empty arrays and singleton dimensions follow MATLAB's column-major semantics.
ctranspose Function GPU Execution Behaviour
When a tensor resides on the GPU, RunMat first asks the active acceleration provider to execute the transpose and conjugation in-device:
- Provider support: If the backend exposes both
transpose(orpermute) andunary_conj, the entire operation happens on the device without a gather. - Partial hooks: If the transpose succeeds but conjugation fails, RunMat falls back to the host path while logging a warning so users know their backend is incomplete.
- No hooks: RunMat gathers the tensor, applies the conjugate transpose on the CPU, and re-uploads the result when possible so downstream kernels can keep running on the GPU.
Current providers operate on real double-precision tensors; complex GPU tensors will gather to the host until native complex layouts are implemented.
Examples of using the ctranspose function in MATLAB / RunMat
Conjugate transpose of a complex matrix
Z = [1+2i 3-4i; 5+0i 6-7i];
H = ctranspose(Z);
Expected output:
H =
1 - 2i 5 - 0i
3 + 4i 6 + 7i
Conjugate transpose of a real matrix equals the plain transpose
A = [1 2 3; 4 5 6];
B = ctranspose(A);
Expected output:
B =
1 4
2 5
3 6
Conjugate transpose turns row vectors into column vectors
row = [1-2i, 3+4i, 5];
col = ctranspose(row);
size(col)
Expected output:
ans = [3 1]
Conjugate transpose of a complex scalar
z = 2 + 3i;
result = ctranspose(z);
Expected output:
result = 2 - 3i;
Conjugate transpose of text data preserves characters
C = ['r' 'u' 'n'; 'm' 'a' 't'];
CT = ctranspose(C);
Expected output:
CT =
'rm'
'ua'
'nt'
Conjugate transpose of a gpuArray without leaving the device
G = gpuArray(rand(1024, 64) + 1i * rand(1024, 64));
GT = ctranspose(G);
GT stays on the GPU when the provider implements the needed hooks; otherwise RunMat gathers,
applies the conjugate transpose, and uploads the result transparently.
GPU residency in RunMat (Do I need gpuArray?)
No additional residency management is required. If the planner keeps your data on the GPU,
ctranspose honours that residency and either executes on the device (when hooks are present) or
performs a gather/transpose/upload round-trip automatically.
FAQ
- How is
ctransposedifferent fromtranspose?
transpose(A.') swaps dimensions without conjugation;ctranspose(A') also conjugates complex values. For purely real data they are identical. - Does
ctransposechange logical or character arrays?
Only their layout changes. Values remain logical or character, and conjugation has no effect. - What about higher-dimensional arrays?
Only the first two axes are swapped; trailing dimensions stay in-place, matching MATLAB. - Does the result share storage with the input?
No.ctransposematerialises a fresh array, although fusion may eliminate the copy in optimised pipelines. - How are complex tensors handled on the GPU today?
Complex gpuArray support is still in flight. When complex data appears, RunMat gathers to the host, applies the conjugate transpose, and re-uploads if needed. - Will
ctransposefuse with neighbouring kernels?
Conjugate transposes currently act as fusion boundaries so that shape changes are visible to downstream kernels. - Can I rely on
ctransposeinside linear-algebra routines (e.g., Hermitian products)?
Yes. The builtin mirrors MATLAB semantics precisely and is safe to use inside idioms likeA' * A. - What error do I get for unsupported types?
Non-numeric objects (e.g., structs) raisectranspose: unsupported input type ..., matching MATLAB's strict type checks.
See Also
transpose, conj, mtimes, gpuArray, gather
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/math/linalg/ops/ctranspose.rs - Found a behavioural difference? Please open an issue with details and a minimal repro.