Encoding signed integers and missing distances

Lax489179.IntegerEncoding · concepts/Lax489179/IntegerEncoding.lean · lax-489179

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Definition

    Signed integers are stored in natural-number words using 0↦00\mapsto0, −1↦1-1\mapsto1, 1↦21\mapsto2, −2↦3-2\mapsto3, and so on. For an optional integer, zero denotes absence and an integer is encoded by its signed code plus one. This separates a missing edge or infinite distance from every finite weight, including zero and negative weights.

    Concept map
    1 concept; 7 descendants hidden
    100%
    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

    Lean source view on GitHub

    1import Mathlib.Data.Int.Basic
    2
    3/-!
    4---
    5title: Encoding signed integers and missing distances
    6type: definition
    7---
    8Signed integers are stored in natural-number words using
    90↦00\mapsto0, −1↦1-1\mapsto1, 1↦21\mapsto2, −2↦3-2\mapsto3, and so on.
    10For an optional integer, zero denotes absence and an integer is encoded
    11by its signed code plus one. This separates a missing edge or infinite
    12distance from every finite weight, including zero and negative weights.
    13-/
    14
    15namespace Lax489179.IntegerEncoding
    16
    17def encodeInt : ℤ → ℕ
    18 | .ofNat n => 2 * n
    19 | .negSucc n => 2 * n + 1
    20
    21def decodeInt (n : ℕ) : ℤ :=
    22 if n % 2 = 0 then .ofNat (n / 2) else .negSucc (n / 2)
    23
    24def encodeOption : Option ℤ → ℕ
    25 | none => 0
    26 | some z => encodeInt z + 1
    27
    28def decodeOption : ℕ → Option ℤ
    29 | 0 => none
    30 | n + 1 => some (decodeInt n)
    31
    32end Lax489179.IntegerEncoding
    33

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…