kigubkur.dig.index
Indexing function returns (if found) the index of a scalar element in a kigubkur© matrix (or block). Note that the index follows kigubkur© indexing strategy; rows indices follow 1, …, m and column indices follow 1, …, n. Thus, index starts from 1 and NOT from 0.
(require '[kigubkur.dig [index :refer [findindex]]])
Examples
Index of an element in a vector
This returns the index such that
return | condition |
---|---|
nil | does not contain the scalar |
s, a natural number | DEFAULT (contains the scalar) |
[s1 s2 ... sk] , a clojure vector of natural numbers | with optional argument "all" (contains multiple) |
=> (def r [[4 3 2 4 0 2 1]])
=> (def c [[4][3][2][4][0][2][1]])
=> (view r)
[4 3 2 4 0 2 1]
Order -> 1 x 7
=> (view c)
[4]
[3]
[2]
[4]
[0]
[2]
[1]
Order -> 7 x 1
If the vector contains a specific number (element)
=> (findindex r 0)
5
=> (findindex c 2)
3
returns a natural number representing position in respective vector (row or column). Note that by default the function returns the first position found. To get all the positions insert the additional argument "all"
=> (findindex c 2 "all")
[3 6]
Finally, if the vector does not contain the scalar the function returns a nil
=> (findindex r 10)
nil
Index of an element in a matrix
Returns the index as
return | condition |
---|---|
nil | does not contain the scalar |
[i j] such that i , j ∈ ℕ | DEFAULT (contains the scalar) |
[[i1 j1] [i2 j2] ... [ik jk]] | with optional argument "all" (contains multiple) |
=> (def M [[0 5 1 0] [2 3 9 5] [9 1 7 0]])
=> (view M)
[0 5 1 0]
[2 3 9 5]
[9 1 7 0]
Order -> 3 x 4
=> (findindex M 10)
nil
=> (findindex M 2)
[2 1]
=> (findindex M 0)
[1 1]
=> (findindex M 0 "all")
[[1 1] [1 4] [3 4]]
Index of an element in a block
Returns the index as
return | condition |
---|---|
nil | does not contain the scalar |
[{:Apq [ip jq]} [i j]] such that p , q , ip , jq , i , j ∈ ℕ | DEFAULT (contains the scalar) |
[{:Aab [ia jb] ... :Apq [ip jq]} [[i1 j1] [i2 j2] ... [ik jk]]] | with optional argument "all" (contains multiple) |
(require '[kigubkur.construct [blocking :refer [block]]])
(require '[kigubkur.elmat [random :refer [urandi]]])
=> (def L (urandi [7 10] [0 11]))
=> (view L)
[6 6 5 10 0 5 7 9 2 0]
[0 3 2 9 7 5 5 8 5 2]
[10 10 1 3 5 3 4 7 8 1]
[10 2 5 2 8 4 7 1 2 6]
[8 4 8 7 2 1 4 4 1 2]
[2 4 3 3 5 10 9 3 3 7]
[9 2 7 2 9 7 5 3 8 1]
Order -> 7 x 10
=> (def B (block L 5))
=> (pprint B)
[[{:A11 [[6 6 5 10 0]
[0 3 2 9 7]
[10 10 1 3 5]
[10 2 5 2 8]
[8 4 8 7 2]],
:A12 [[5 7 9 2 0]
[5 5 8 5 2]
[3 4 7 8 1]
[4 7 1 2 6]
[1 4 4 1 2]]}]
[{:A22 [[10 9 3 3 7]
[7 5 3 8 1]],
:A21 [[2 4 3 3 5]
[9 2 7 2 9]]}]]
=> (findindex B 100)
nil
=> (findindex B 1)
[{:A22 [2 5], :A11 [3 3], :A12 [3 5]} [3 3]]
=> (findindex B 1 "all")
[{:A22 [[2 5]], :A11 [[3 3]], :A12 [[3 5] [4 3] [5 1] [5 4]]} [[3 3] [3 10] [4 8] [5 6] [5 9] [7 10]]]
findindex
(findindex M x & arg)
Returns index (position) of scalar x
in vector/matrix/block M
. Optional argument is "all"
.
Syntax | condition | return for vector (row, column) M | return for matrix M | return for block M |
---|---|---|---|---|
(findindex M x) | does not contain the scalar x | nil | nil | nil |
(findindex M x) | DEFAULT (contains the scalar x ) | s, a natural number | [i j] such that i , j ∈ ℕ | [{:Apq [ip jq]} [i j]] such that p , q , ip , jq , i , j ∈ ℕ |
(findindex M x "all") | contains >1 x | [s1 s2 ... sk] , a clojure vector of natural numbers | [[i1 j1] [i2 j2] ... [ik jk]] | [{:Aab [ia jb] ... :Apq [ip jq]} [[i1 j1] [i2 j2] ... [ik jk]]] |