- 1. matrix
- 1.1. Syntax
- 1.2. Stack Effects
- 1.3. Description
- 1.4. PostScript Level
- 1.5. Examples
- 1.6. Common Use Cases
- 1.7. Common Pitfalls
- 1.8. Error Conditions
- 1.9. Implementation Notes
- 1.10. Identity Matrix Properties
- 1.11. Matrix Format
- 1.12. Memory Management
- 1.13. Performance Considerations
- 1.14. Relationship to Other Operators
- 1.15. See Also
1. matrix
Creates an identity transformation matrix.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| Level | Object |
|---|---|
0 |
|
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.5. Examples
matrix % → [1.0 0.0 0.0 1.0 0.0 0.0]
matrix % Start with identity
100 200 translate % Modify to translation matrix
% → [1.0 0.0 0.0 1.0 100.0 200.0]
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.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 |
|---|---|
[ |
Operand stack overflow |
[ |
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
-
identmatrix- Initialize array as identity matrix -
currentmatrix- Get current transformation matrix -
defaultmatrix- Get device default matrix -
setmatrix- Set transformation matrix -
concat- Concatenate matrix with CTM -
concatmatrix- Multiply two matrices -
array- Create array