Rebol3 Code Examplex


Display a linear combination

Show how a value can be expressed as a weighted sum of terms.

Rebol [
    title: "Rosetta code: Display a linear combination"
    file:  %Display_a_linear_combination.r3
    url:    https://rosettacode.org/wiki/Display_a_linear_combination
    needs: 3.10.0 ;; or something like that (pad/ajoin)
]

display-linear-combination: function [
    vec [block!]   "Vector of coefficients"
][
    terms: copy ""
    repeat i length? vec [
        coeff: vec/:i
        if coeff <> 0 [
            ;; build "c * e(k)" term
            append terms ajoin [
                unless empty? terms [" + "]
                pad coeff -2
                " * e(" i ")"
            ]
        ]
    ]
    print ajoin [
        pad mold vec -15 ;; padded input
        " -> "
        terms
    ]
]

;; Test output...
foreach v [
    [1 2 3]
    [0 1 2 3]
    [1 0 3 4]
    [1 2 0]
    [0 0 0]
    [0]
    [1 1 1]
    [-1 -1 -1]
    [-1 -2 0 -3]
    [-1]
][
    display-linear-combination v
]

Output:

        [1 2 3] ->  1 * e(1) +  2 * e(2) +  3 * e(3)
      [0 1 2 3] ->  1 * e(2) +  2 * e(3) +  3 * e(4)
      [1 0 3 4] ->  1 * e(1) +  3 * e(3) +  4 * e(4)
        [1 2 0] ->  1 * e(1) +  2 * e(2)
        [0 0 0] -> 
            [0] -> 
        [1 1 1] ->  1 * e(1) +  1 * e(2) +  1 * e(3)
     [-1 -1 -1] -> -1 * e(1) + -1 * e(2) + -1 * e(3)
   [-1 -2 0 -3] -> -1 * e(1) + -2 * e(2) + -3 * e(4)
           [-1] -> -1 * e(1)