Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  monge.rs

  Sprache: Rust
 

//! Functions for generating and checking Monge arrays.
//!
//! The functions here are mostly meant to be used for testing
//! correctness of the SMAWK implementation.

use crate::Matrix;
use std::num::Wrapping;
use std::ops::Add;

/// Verify that a matrix is a Monge matrix.
///
/// A [Monge matrix] \(or array) is a matrix where the following
/// inequality holds:
///
/// ```text
/// M[i, j] + M[i', j'] <= M[i, j'] + M[i', j]  for all i < i', j < j'
/// ```
///
/// The inequality says that the sum of the main diagonal is less than
/// the sum of the antidiagonal. Checking this condition is done by
/// checking *n* ✕ *m* submatrices, so the running time is O(*mn*).
///
/// [Monge matrix]: https://en.wikipedia.org/wiki/Monge_array
pub fn is_monge<T: Ord + Copy, M: Matrix<T>>(matrix: &M) -> bool
where
    Wrapping<T>: Add<Output = Wrapping<T>>,
{
    /// Returns `Ok(a + b)` if the computation can be done without
    /// overflow, otherwise `Err(a + b - T::MAX - 1)` is returned.
    fn checked_add<T: Ord + Copy>(a: Wrapping<T>, b: Wrapping<T>) -> Result<T, T>
    where
        Wrapping<T>: Add<Output = Wrapping<T>>,
    {
        let sum = a + b;
        if sum < a {
            Err(sum.0)
        } else {
            Ok(sum.0)
        }
    }

    (0..matrix.nrows() - 1)
        .flat_map(|row| (0..matrix.ncols() - 1).map(move |col| (row, col)))
        .all(|(row, col)| {
            let top_left = Wrapping(matrix.index(row, col));
            let top_right = Wrapping(matrix.index(row, col + 1));
            let bot_left = Wrapping(matrix.index(row + 1, col));
            let bot_right = Wrapping(matrix.index(row + 1, col + 1));

            match (
                checked_add(top_left, bot_right),
                checked_add(bot_left, top_right),
            ) {
                (Ok(a), Ok(b)) => a <= b,   // No overflow.
                (Err(a), Err(b)) => a <= b, // Double overflow.
                (Ok(_), Err(_)) => true,    // Anti-diagonal overflow.
                (Err(_), Ok(_)) => false,   // Main diagonal overflow.
            }
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_monge_handles_overflow() {
        // The x + y <= z + w computations will overflow for an u8
        // matrix unless is_monge is careful.
        let matrix: Vec<Vec<u8>> = vec![
            vec![200200200200],
            vec![200200200200],
            vec![200200200200],
        ];
        assert!(is_monge(&matrix));
    }

    #[test]
    fn monge_constant_rows() {
        let matrix = vec![
            vec![42424242],
            vec![0000],
            vec![100100100100],
            vec![1000100010001000],
        ];
        assert!(is_monge(&matrix));
    }

    #[test]
    fn monge_constant_cols() {
        let matrix = vec![
            vec![4201001000],
            vec![4201001000],
            vec![4201001000],
            vec![4201001000],
        ];
        assert!(is_monge(&matrix));
    }

    #[test]
    fn monge_upper_right() {
        let matrix = vec![
            vec![1010424242],
            vec![1010424242],
            vec![1010101010],
            vec![1010101010],
        ];
        assert!(is_monge(&matrix));
    }

    #[test]
    fn monge_lower_left() {
        let matrix = vec![
            vec![1010101010],
            vec![1010101010],
            vec![4242421010],
            vec![4242421010],
        ];
        assert!(is_monge(&matrix));
    }
}

Messung V0.5 in Prozent
C=71 H=95 G=83

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-06-18) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik