kigubkur.op.edivide
Operate: element-wise division (applicable to blocks).
edivide
(require '[kigubkur.op [edivide :refer [edivide]]])
Disregarding the − operator in the definition for subtraction and replacing it with ÷ the definition for the element-wise division operator edivide
is analogous. The edivide
operator is also akin to the right-division “∕” (as opposed to the left-division “∖”). Thus,
Note that in (edivide A B)
is equal to A “∕” B where A is the dividend and B is the divisor.
Case 1: scalar s is the dividend or the divisor
Case 2: between row r and column c
Case 3: between row r and matrix A
Case 4: between column c and matrix A
Case 5: blocks of same order
Case 6: between different ordered block-row and block-column
Case 6.1; mother of block-column is a column matrix (vector)
Case 6.2; mother of block-column is a matrix
Case 7: between different ordered block-matrix and block-row
Case 8: between different ordered block-matrix and block-column
Examples
Scalar as dividend or divisor
Scalar divided from scalar
=> (edivide 3 2)
3/2
=> (edivide 3 2.0)
1.5
Note that kigubkur© supports Clojure’s numbers. The above 3/2
is an example of Clojure’s ratio or rational number. For all the most basic operators in kigubkur© like addition, subtraction, multiplication and division the respective operations are done on the given numbers, that is, the numbers are not converted (say, into floats) for the operation. This is most evident during division (as shown above). Therefore a fraction of integers, i.e., a rational number does not get reduced to real numbers (say, floats) or integers. Therefore, provided that one uses only integers and rational numbers the basic operations in kigubkur© are inherently non-lossy in terms for preserving the precision.
Scalar division from row matrix
=> (def r1 [[1 2 3 4]])
=> (def r2 [[4 3 2 1]])
=> (view r1)
[1 2 3 4]
Order -> 1 x 4
=> (view (edivide r1 2))
[1/2 1 3/2 2]
Order -> 1 x 4
Division is not commutative.
=> (view (edivide 2 r1))
[2 1 2/3 1/2]
Order -> 1 x 4
Scalar division from column matrix
=> (def c1 [[1][2][3][4]])
=> (def c2 [[4][3][2][1]])
=> (view c1)
[1]
[2]
[3]
[4]
Order -> 4 x 1
=> (view (edivide c1 2))
[1/2]
[1]
[3/2]
[2]
Order-> 4 x 1
Division is not commutative.
=> (view (edivide 2 c1))
[2]
[1]
[2/3]
[1/2]
Order -> 4 x 1
Scalar division from matrix
=> (def M1 [[1 2] [3 4]])
=> (def M2 [[4 3] [2 1]])
=> (view M1)
[1 2]
[3 4]
Order -> 2 x 2
=> (view (edivide M1 2))
[1/2 1]
[3/2 2]
Order -> 2 x 2
Division is not commutative.
=> (view (edivide 2 M1))
[2 1]
[2/3 1/2]
Order -> 2 x 2
Scalar division from block matrix
(require '[kigubkur.construct [blocking :refer [block]]])
=> (def L1 [[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]])
=> (def B1 (block L1 5))
=> (def L2 [[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]])
=> (def B2 (block L2 5))
=> (pprint B1)
[[{:A11 [[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]],
:A12 [[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]]}]
[{:A22 [[6 7 8 9 10]
[6 7 8 9 10]],
:A21 [[1 2 3 4 5]
[1 2 3 4 5]]}]]
=> (pprint (edivide B1 2))
[[{:A11 [[1/2 1 3/2 2 5/2]
[1/2 1 3/2 2 5/2]
[1/2 1 3/2 2 5/2]
[1/2 1 3/2 2 5/2]
[1/2 1 3/2 2 5/2]],
:A12 [[3 7/2 4 9/2 5]
[3 7/2 4 9/2 5]
[3 7/2 4 9/2 5]
[3 7/2 4 9/2 5]
[3 7/2 4 9/2 5]]}]
[{:A22 [[3 7/2 4 9/2 5]
[3 7/2 4 9/2 5]],
:A21 [[1/2 1 3/2 2 5/2]
[1/2 1 3/2 2 5/2]]}]]
Division is not commutative.
=> (pprint (edivide 2 B1))
[[{:A11 [[2 1 2/3 1/2 2/5]
[2 1 2/3 1/2 2/5]
[2 1 2/3 1/2 2/5]
[2 1 2/3 1/2 2/5]
[2 1 2/3 1/2 2/5]],
:A12 [[1/3 2/7 1/4 2/9 1/5]
[1/3 2/7 1/4 2/9 1/5]
[1/3 2/7 1/4 2/9 1/5]
1/3 2/7 1/4 2/9 1/5]
[1/3 2/7 1/4 2/9 1/5]]}]
[{:A22 [[1/3 2/7 1/4 2/9 1/5]
[1/3 2/7 1/4 2/9 1/5]],
:A21 [[2 1 2/3 1/2 2/5]
[2 1 2/3 1/2 2/5]]}]]
Dividend and divisor is not a scalar
Row matrix division from row matrix
=> (view r1)
[1 2 3 4]
Order -> 1 x 4
=> (view r2)
[4 3 2 1]
Order -> 1 x 4
=> (view (edivide r1 r2))
[1/4 2/3 3/2 4]
Order -> 1 x 4
=> (view (edivide r2 r1))
[4 3/2 2/3 1/4]
Order -> 1 x 4
Row division from matrix or vice versa
=> (view M1)
[1 2]
[3 4]
Order -> 2 x 2
=> (view (edivide M1 [[1 2]]))
[1 1]
[3 2]
Order -> 2 x 2
=> (view (edivide [[1 2]] M1))
[1 1]
[1/3 1/2]
Order -> 2 x 2
Column matrix division from column matrix
=> (view c1)
[1]
[2]
[3]
[4]
Order -> 4 x 1
=> (view c2)
[4]
[3]
[2]
[1]
Order -> 4 x 1
=> (view (edivide c1 c2))
[1/4]
[2/3]
[3/2]
[4]
Order -> 4 x 1
=> (view (edivide c2 c1))
[4]
[3/2]
[2/3]
[1/4]
Order -> 4 x 1
Column division from matrix or vice versa
=> (view M2)
[4 3]
[2 1]
Order -> 2 x 2
=> (view (edivide M2 [[1][2]]))
[4 3]
[1 1/2]
Order -> 2 x 2
=> (view (edivide [[1][2]] M2))
[1/4 1/3]
[1 2]
Order -> 2 x 2
Column matrix division from row matrix or vice versa
=> (view (edivide r1 c1))
[1 2 3 4]
[1/2 1 3/2 2]
[1/3 2/3 1 4/3]
[1/4 1/2 3/4 1]
Order -> 4 x 4
=> (view (edivide c1 r1))
[1 1/2 1/3 1/4]
[2 1 2/3 1/2]
[3 3/2 1 3/4]
[4 2 4/3 1]
Order -> 4 x 4
Matrix division from Matrix
=> (view M1)
[1 2]
[3 4]
Order -> 2 x 2
=> (view M2)
[4 3]
[2 1]
Order -> 2 x 2
=> (view (edivide M1 M2))
[1/4 2/3]
[3/2 4]
Order -> 2 x 2
=> (view (edivide M2 M1))
[4 3/2]
[2/3 1/4]
Order -> 2 x 2
Block division from block
=> (pprint B1)
[[{:A11 [[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]],
:A12 [[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]]}]
[{:A22 [[6 7 8 9 10]
[6 7 8 9 10]],
:A21 [[1 2 3 4 5]
[1 2 3 4 5]]}]]
=> (pprint B2)
[[{:A11 [[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]],
:A12 [[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]]}]
[{:A21 [[10 9 8 7 6]
[10 9 8 7 6]],
:A22 [[5 4 3 2 1]
[5 4 3 2 1]]}]]
=> (pprint (edivide B1 B2))
[[{:A11 [[1/10 2/9 3/8 4/7 5/6]
[1/10 2/9 3/8 4/7 5/6]
[1/10 2/9 3/8 4/7 5/6]
[1/10 2/9 3/8 4/7 5/6]
[1/10 2/9 3/8 4/7 5/6]],
:A12 [[6/5 7/4 8/3 9/2 10]
[6/5 7/4 8/3 9/2 10]
[6/5 7/4 8/3 9/2 10]
[6/5 7/4 8/3 9/2 10]
[6/5 7/4 8/3 9/2 10]]}]
[{:A22 [[6/5 7/4 8/3 9/2 10]
[6/5 7/4 8/3 9/2 10]],
:A21 [[1/10 2/9 3/8 4/7 5/6]
[1/10 2/9 3/8 4/7 5/6]]}]]
=> (pprint (edivide B2 B1))
[[{:A11 [[10 9/2 8/3 7/4 6/5]
[10 9/2 8/3 7/4 6/5]
[10 9/2 8/3 7/4 6/5]
[10 9/2 8/3 7/4 6/5]
[10 9/2 8/3 7/4 6/5]],
:A12 [[5/6 4/7 3/8 2/9 1/10]
[5/6 4/7 3/8 2/9 1/10]
[5/6 4/7 3/8 2/9 1/10]
[5/6 4/7 3/8 2/9 1/10]
[5/6 4/7 3/8 2/9 1/10]]}]
[{:A22 [[5/6 4/7 3/8 2/9 1/10]
[5/6 4/7 3/8 2/9 1/10]],
:A21 [[10 9/2 8/3 7/4 6/5]
[10 9/2 8/3 7/4 6/5]]}]]
edivide
(edivide X Y)
Given a scalar, row matrix, column matrix, matrix or a block and a scalar, row matrix, column matrix, matrix or a block, returns element-wise division.
Syntax: (edivide X Y)
s.t X
, Y
is a scalar or kigubkur© matrix (or block)
Note:
if xik or yik ∈ {##NaN
, ##Inf
, ##-Inf
}, returns ##NaN
,
otherwise, returns x ÷ y.