1. matrix

Creates an identity transformation matrix.

1.1. Syntax

- matrix → matrix

1.2. Stack Effects

Table 1. Before
Level Object

(empty)

No operands required

Table 2. After
Level Object

0

matrix (6-element array)

1.3. Description

matrix creates a 6-element array object, fills it with the values of an identity matrix [1.0 0.0 0.0 1.0 0.0 0.0], and pushes this array on the operand stack.

The array is allocated in local or global VM according to the current VM allocation mode.

The identity matrix transforms any coordinate to itself without change.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Creating an identity matrix
matrix  % → [1.0 0.0 0.0 1.0 0.0 0.0]
Building a transformation matrix
matrix              % Start with identity
100 200 translate  % Modify to translation matrix
% → [1.0 0.0 0.0 1.0 100.0 200.0]
Multiple matrices
matrix /m1 exch def
matrix /m2 exch def
% Now have two independent matrices

1.6. Common Use Cases

1.6.1. Saving Current Transformation

matrix currentmatrix  % Save CTM
% ... perform transformations ...
setmatrix             % Restore CTM

1.6.2. Building Custom Transformations

% Build transformation incrementally
matrix
50 100 translate   % Add translation
2 2 scale          % Add scaling
45 rotate          % Add rotation
% Matrix now contains combined transformation

1.6.3. Matrix Arithmetic

% Multiply two transformation matrices
/m1 matrix 100 0 translate def
/m2 matrix 45 rotate def
/m3 matrix def
m1 m2 m3 concatmatrix  % m3 = m1 × m2

1.6.4. Temporary Transformations

% Apply transformation without affecting CTM
matrix currentmatrix  % Save CTM
/savedCTM exch def

100 100 translate
2 2 scale
% ... draw content ...

savedCTM setmatrix   % Restore CTM

1.7. Common Pitfalls

Not Just Array Creation - matrix creates an array AND fills it with identity matrix values.
% These are different:
matrix              % → [1.0 0.0 0.0 1.0 0.0 0.0]
6 array             % → [null null null null null null]
VM Allocation - Matrix arrays are allocated in VM and consume memory.
% Creating many matrices can consume VM
1 1 1000 {
  pop
  matrix  % Each creates a new array
} for
% Better: reuse matrices when possible
Reuse Matrices - For efficiency, reuse matrix arrays rather than creating new ones:
% Create once:
/myMatrix matrix def

% Reuse multiple times:
myMatrix currentmatrix pop
myMatrix 100 100 translate pop
Equivalent to Array Creation - This is equivalent to matrix:
6 array identmatrix  % Same as: matrix

1.8. Error Conditions

Error Condition

[stackoverflow]

Operand stack overflow

[VMerror]

Insufficient VM for array allocation

1.9. Implementation Notes

  • The created array always has exactly 6 elements

  • Elements are initialized to real numbers (1.0, 0.0) even if integers would suffice

  • The array is a standard PostScript array, not a special matrix type

  • The array can be modified like any other array

  • Created arrays are subject to normal garbage collection

1.10. Identity Matrix Properties

The identity matrix [1 0 0 1 0 0] has these properties:

  • Transforms any point (x, y) to itself: (x', y') = (x, y)

  • Is the multiplicative identity: I × M = M × I = M

  • Has a determinant of 1

  • Is its own inverse: I⁻¹ = I

1.11. Matrix Format

The 6-element matrix array represents:

[a b c d tx ty]

Which transforms coordinates:

x' = a×x + c×y + tx
y' = b×x + d×y + ty

For the identity matrix [1 0 0 1 0 0]:

x' = 1×x + 0×y + 0 = x
y' = 0×x + 1×y + 0 = y

1.12. Memory Management

% Matrices are composite objects
/m1 matrix def
/m2 m1 def        % m2 shares value with m1
m1 0 99 put       % Modifies both m1 and m2

% To create independent copy:
/m1 matrix def
/m2 m1 length array def
m1 m2 copy pop    % m2 is independent copy

1.13. Performance Considerations

  • Creating a matrix is a lightweight operation

  • The cost is similar to creating any 6-element array

  • Reusing matrices is more efficient than creating new ones

  • Matrix operations (concat, transform) are optimized

1.14. Relationship to Other Operators

% These are equivalent:
matrix

6 array identmatrix

[1.0 0.0 0.0 1.0 0.0 0.0]  % Literal array

% Common usage pattern:
matrix currentmatrix  % Get CTM into new matrix

1.15. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.