Crate diagonal

source ·
Expand description

§Diagonal Operations Module

This module provides functions to extract diagonals from a matrix in various directions.

§Diagonals with Positive Slope (Bottom-Left to Top-Right)

The diagonal_pos_pos function extracts diagonals with positive slope from a matrix starting from the bottom-left corner (x: maximum, y: 0).

§Example

use diagonal::diagonal_pos_pos;

let matrix = vec![
    vec![1, 2, 3],
    vec![4, 5, 6],
    vec![7, 8, 9],
];

let result = diagonal_pos_pos(&matrix);
assert_eq!(result, vec![
    vec![&7],
    vec![&4, &8],
    vec![&1, &5, &9],
    vec![&2, &6],
    vec![&3],
]);

§Diagonals with Positive and Negative Slope (Top-Left to Bottom-Right and Bottom-Left to Top-Right)

The diagonal_pos_neg function extracts diagonals with positive and negative slope from a matrix starting from the top-left corner (x & y: 0).

§Example

use diagonal::diagonal_pos_neg;

let matrix = vec![
    vec![1, 2, 3],
    vec![4, 5, 6],
    vec![7, 8, 9],
];

let result = diagonal_pos_neg(&matrix);
assert_eq!(result, vec![
    vec![&1],
    vec![&2, &4],
    vec![&3, &5, &7],
    vec![&6, &8],
    vec![&9],
]);

§Straight Rows and Columns

The straight_x and straight_y functions extract elements from a matrix in row-major and column-major orders, respectively.

§Example

use diagonal::{straight_x, straight_y};

let matrix = vec![
    vec![1, 2, 3],
    vec![4, 5, 6],
    vec![7, 8, 9],
];

let result_x = straight_x(&matrix);
assert_eq!(result_x, vec![
    vec![&1, &2, &3],
    vec![&4, &5, &6],
    vec![&7, &8, &9],
]);

let result_y = straight_y(&matrix);
assert_eq!(result_y, vec![
    vec![&1, &4, &7],
    vec![&2, &5, &8],
    vec![&3, &6, &9],
]);

The provided functions enable convenient extraction and manipulation of matrix diagonals, making it easier to perform various operations on matrix elements.

§Note

Make sure to use the diagonal crate and import the necessary functions for these examples to work. Add the following to your Cargo.toml file:

[dependencies]
diagonal = "0.1.0"

Functions§

  • Extracts diagonals with positive and negative slope from a matrix starting from the top-left (x & y: 0) corner.
  • Extracts diagonals with positive slope from a matrix starting from the bottom-left (x: maximum, y: 0) corner.
  • Extracts elements from a matrix in a row-major order and organizes them into vectors, where each vector represents a row of the original matrix.
  • Extracts elements from a matrix in a column-major order and organizes them into vectors, where each vector represents a column of the original matrix.