| Crates.io | diagonal |
| lib.rs | diagonal |
| version | 0.1.0 |
| created_at | 2024-03-04 23:01:20.562775+00 |
| updated_at | 2024-03-04 23:01:20.562775+00 |
| description | Extract diagonals from a matrix in various directions |
| homepage | |
| repository | https://github.com/Antosser/diagonal-rs |
| max_upload_size | |
| id | 1162371 |
| size | 18,899 |
This Rust module provides functions to perform operations on diagonals in matrices. It includes functions for extracting diagonals with positive slope, diagonals with positive and negative slope, as well as straight rows and columns.
The diagonal_pos_pos function extracts diagonals with positive slope from a matrix starting from the bottom-left corner (x: maximum, y: 0).
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],
]);
The diagonal_pos_neg function extracts diagonals with positive and negative slope from a matrix starting from the top-left corner (x & y: 0).
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],
]);
The straight_x and straight_y functions extract elements from a matrix in row-major and column-major orders, respectively.
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.
To use this module, add the diagonal crate to your Cargo.toml file:
[dependencies]
diagonal = "0.1.0"
Now, you can import the necessary functions and start working with matrix diagonals in Rust!