-
Notifications
You must be signed in to change notification settings - Fork 1
Matrix/Vector coordinate list conversions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f7d2a29
Add vector conversion from coordinate list
Danil-Zaripov 31dba1e
Adjust for dummies creation
Danil-Zaripov e0a7a1e
Add sparse matrix COO convertions
Danil-Zaripov 6940daa
Add matrix tests
Danil-Zaripov 55538ec
Rename predicate
Danil-Zaripov 6994abc
Add measures to matrix indices
Danil-Zaripov a6212f0
Add condensation of empty matrix test
Danil-Zaripov c2bd261
Add condensation of sparse matrix test
Danil-Zaripov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,98 @@ let mkNode x1 x2 x3 x4 = | |
| | Leaf(v1), Leaf(v2), Leaf(v3), Leaf(v4) when v1 = v2 && v2 = v3 && v3 = v4 -> Leaf(v1) | ||
| | _ -> Node(x1, x2, x3, x4) | ||
|
|
||
| [<Measure>] | ||
| type rowindex | ||
|
|
||
| [<Measure>] | ||
| type colindex | ||
|
|
||
| type COOEntry<'value> = uint64<rowindex> * uint64<colindex> * 'value | ||
|
|
||
| [<Struct>] | ||
| type CoordinateList<'value> = | ||
| val nrows: uint64<nrows> | ||
| val ncols: uint64<ncols> | ||
| val list: COOEntry<'value> list | ||
|
|
||
| new(_nrows, _ncols, _list) = | ||
| { nrows = _nrows | ||
| ncols = _ncols | ||
| list = _list } | ||
|
|
||
| let private getQuadrantCoords (pr, pc) halfSize = | ||
| (pr, pc), // NORTH WEST | ||
| (pr, pc + halfSize * 1UL<colindex>), // NORTH EAST | ||
| (pr + halfSize * 1UL<rowindex>, pc), // SOUTH WEST | ||
| (pr + halfSize * 1UL<rowindex>, pc + halfSize * 1UL<colindex>) // SOUTH EAST | ||
|
|
||
| let fromCoordinateList (coo: CoordinateList<'a>) = | ||
| let nvals = (uint64 <| List.length coo.list) * 1UL<nvals> | ||
| let nrows = coo.nrows | ||
| let ncols = coo.ncols | ||
|
|
||
| // the resulting matrix is always square | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хорошоая ли это идея? А если матрица достаточно сильно прямоугольная? Скажем, 3х1000 ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А. Или тогда будет более сложное дерево в итоге? |
||
| let storageSize = getNearestUpperPowerOfTwo (max (uint64 nrows) (uint64 ncols)) | ||
|
|
||
| let isEntryInQuadrant (pr, pc) size (entry: COOEntry<'a>) = | ||
| let (i, j, _) = entry | ||
|
|
||
| i >= pr | ||
| && j >= pc | ||
| && i < pr + size * 1UL<rowindex> | ||
| && j < pc + size * 1UL<colindex> | ||
|
|
||
| let rec traverse coordinates (pr, pc) size = | ||
| match coordinates with | ||
| | [] when (uint64 pr) + size < uint64 nrows && (uint64 pc) + size < uint64 ncols -> Leaf <| UserValue None | ||
| | [] when uint64 pr >= uint64 nrows || uint64 pc >= uint64 ncols -> Leaf Dummy | ||
| | (i, j, value) :: _ when pr = i && pc = j && size = 1UL -> Leaf << UserValue <| Some value | ||
| | _ -> | ||
| let halfSize = size / 2UL | ||
| let nwp, nep, swp, sep = getQuadrantCoords (pr, pc) halfSize | ||
| let nwCoo = coordinates |> List.filter (isEntryInQuadrant nwp halfSize) | ||
| let neCoo = coordinates |> List.filter (isEntryInQuadrant nep halfSize) | ||
| let swCoo = coordinates |> List.filter (isEntryInQuadrant swp halfSize) | ||
| let seCoo = coordinates |> List.filter (isEntryInQuadrant sep halfSize) | ||
|
|
||
| mkNode | ||
| (traverse nwCoo nwp halfSize) | ||
| (traverse neCoo nep halfSize) | ||
| (traverse swCoo swp halfSize) | ||
| (traverse seCoo sep halfSize) | ||
|
|
||
| let tree = traverse coo.list (0UL<rowindex>, 0UL<colindex>) storageSize | ||
|
|
||
| SparseMatrix(nrows, ncols, nvals, Storage(storageSize * 1UL<storageVSize>, storageSize * 1UL<storageHSize>, tree)) | ||
|
|
||
| let toCoordinateList (matrix: SparseMatrix<'a>) = | ||
| let nrows = matrix.nrows | ||
| let ncols = matrix.ncols | ||
|
|
||
| let rec traverse tree (pr, pc) size = | ||
| match tree with | ||
| | Leaf Dummy | ||
| | Leaf(UserValue None) -> [] | ||
| | Leaf(UserValue(Some value)) -> | ||
| [ for i in uint64 pr .. (uint64 pr) + size - 1UL do | ||
| for j in uint64 pc .. (uint64 pc) + size - 1UL -> (i * 1UL<rowindex>, j * 1UL<colindex>, value) ] | ||
| | Node(nw, ne, sw, se) -> | ||
| let halfSize = size / 2UL | ||
| let nwp, nep, swp, sep = getQuadrantCoords (pr, pc) halfSize | ||
|
|
||
| traverse nw nwp halfSize | ||
| @ traverse ne nep halfSize | ||
| @ traverse sw swp halfSize | ||
| @ traverse se sep halfSize | ||
|
|
||
| let coo = | ||
| traverse | ||
| matrix.storage.data | ||
| (0UL<rowindex>, 0UL<colindex>) | ||
| (max (uint64 matrix.storage.hSize) (uint64 matrix.storage.vSize)) | ||
|
|
||
| CoordinateList(nrows, ncols, coo) | ||
|
|
||
| let map2 (matrix1: SparseMatrix<_>) (matrix2: SparseMatrix<_>) f = | ||
| let rec inner (vSize: uint64<storageVSize>) (hSize: uint64<storageHSize>) matrix1 matrix2 = | ||
| let _do x1 x2 x3 x4 y1 y2 y3 y4 = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут тоже не помешал бы тест на "схлопывание" одинаковых значений. Причём не тольео None или Dummy.