It is often useful to apply a pointwise operation to two or more arrays in a many-to-one manner. Library support for this, which math/array provides, is called broadcasting.
- : Indexes
'#(6 6)
> diag- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[1 0 0 0 0 0]
#[0 1 0 0 0 0]
#[0 0 1 0 0 0]
#[0 0 0 1 0 0]
#[0 0 0 0 1 0]
#[0 0 0 0 0 1]])
- : Indexes
'#()
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[10 0 0 0 0 0]
#[0 10 0 0 0 0]
#[0 0 10 0 0 0]
#[0 0 0 10 0 0]
#[0 0 0 0 10 0]
#[0 0 0 0 0 10]])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Nonnegative-Fixnum))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[10 1 2 3 4 5]
#[0 11 2 3 4 5]
#[0 1 12 3 4 5]
#[0 1 2 13 4 5]
#[0 1 2 3 14 5]
#[0 1 2 3 4 15]])
The shorter shape is padded on the left with 1 until it is the same length as the longer shape.
For each axis k, dk and ek are compared. If dk= ek, the result axis is dk; if one axis is length 1, the result axis is the length of the other; otherwise fail.
Both arrays’ axes are stretched by (conceptually) copying the rows of axes with length 1.
es is padded to get #(133).
The result axis is derived from #(413) and #(133) to get #(433).
drr’s second axis is stretched to length 3, and err’s new first axis (which is length 1 by rule 1) is stretched to length 4.
#[#["10""11""12"]]#[#["20""21""22"]]#[#["30""31""32"]]]))- : Indexes
'#(4 1 3)
- : Indexes
'#(3 3)
- : Indexes
'#(4 3 3)
> drr+err- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[#["00aa" "01ab" "02ac"]
#["00ba" "01bb" "02bc"]
#["00ca" "01cb" "02cc"]]
#[#["10aa" "11ab" "12ac"]
#["10ba" "11bb" "12bc"]
#["10ca" "11cb" "12cc"]]
#[#["20aa" "21ab" "22ac"]
#["20ba" "21bb" "22bc"]
#["20ca" "21cb" "22cc"]]
#[#["30aa" "31ab" "32ac"]
#["30ba" "31bb" "32bc"]
#["30ca" "31cb" "32cc"]]])
> ds- : Indexes
'#(4 3 3)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[#["00" "01" "02"]
#["00" "01" "02"]
#["00" "01" "02"]]
#[#["10" "11" "12"]
#["10" "11" "12"]
#["10" "11" "12"]]
#[#["20" "21" "22"]
#["20" "21" "22"]
#["20" "21" "22"]]
#[#["30" "31" "32"]
#["30" "31" "32"]
#["30" "31" "32"]]])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array
#[#[#["aa" "ab" "ac"]
#["ba" "bb" "bc"]
#["ca" "cb" "cc"]]
#[#["aa" "ab" "ac"]
#["ba" "bb" "bc"]
#["ca" "cb" "cc"]]
#[#["aa" "ab" "ac"]
#["ba" "bb" "bc"]
#["ca" "cb" "cc"]]
#[#["aa" "ab" "ac"]
#["ba" "bb" "bc"]
#["ca" "cb" "cc"]]])
array-shape-broadcast: incompatible array shapes
(array-broadcasting #f): '#(3 3), '#()
> arr10- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"])
> arr3- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #["0" "1" "2"])
array-shape-broadcast: incompatible array shapes
(array-broadcasting #t): '#(10), '#(2), '#(3)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #["0+0" "1-1" "2+2" "3-0" "4+1" "5-2" "6+0" "7-1" "8+2" "9-0"])