-
Notifications
You must be signed in to change notification settings - Fork 2
Approximator Abstract Type: Selector #120
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
Open
nathanielpritchard
wants to merge
12
commits into
main
Choose a base branch
from
v0.2-selector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
30b1dd2
added abstract types for CUR
nathanielpritchard 006379a
added selector abstract types
nathanielpritchard 9a00dad
adding forgotten selectors markdown
nathanielpritchard 99b004e
added code for an LU with partial pivoting selector
nathanielpritchard fb81e89
fastforwarded with main
nathanielpritchard 0a55ffb
Merge branch 'v0.2-null_compressor' into v0.2-selector
nathanielpritchard 94ffc2f
modified compressor to allow for sketched LU
nathanielpritchard 7c1757c
appropriately named selector directory
nathanielpritchard 30b0a3a
corrected misspecified link
nathanielpritchard 7edf267
corrected terminology in documentation
nathanielpritchard c7e27a0
merged with main
nathanielpritchard 2ec0fd6
corrected typos and added update_compressor test
nathanielpritchard 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Selectors | ||
| ```@contents | ||
| Pages = ["selectors.md"] | ||
| ``` | ||
|
|
||
| ## Abstract Types | ||
| ```@docs | ||
| Selector | ||
| SelectorRecipe | ||
| ``` | ||
|
|
||
| ## Selector Structures | ||
| ```@docs | ||
| RLinearAlgebra.LUPP | ||
| LUPPRecipe | ||
| ``` | ||
|
|
||
| ## Exported Functions | ||
| ```@docs | ||
| complete_selector | ||
| update_selector! | ||
| select_indices! | ||
| ``` | ||
|
|
||
| ## Internal Functions | ||
| ```@docs | ||
| ``` |
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| ################################### | ||
| # Abstract Types | ||
| ################################### | ||
| """ | ||
| Selector | ||
|
|
||
| An abstract type containing user controlled parameters for a technique that selects indices | ||
| from a matrix. | ||
| """ | ||
| abstract type Selector end | ||
|
|
||
| """ | ||
| SelectorRecipe | ||
|
|
||
| An abstract type containing user controlled parameters and preallocated memory for a | ||
| technique that selects indices from a matrix. | ||
| """ | ||
| abstract type SelectorRecipe end | ||
|
|
||
| select_arg_list = Dict{Symbol,String}( | ||
| :selector => "`selector::Selector`, a data structure containing the user-defined | ||
| parameters associated with a particular selection method.", | ||
| :selector_recipe => "`selector_recipe::SelectorRecipe`, a fully initialized realization | ||
| for a selector method for a particular matrix.", | ||
| :idx => "`idx::vector`, a vector where selected indices will be placed.", | ||
| :n_idx => "`n_idx::Int64`, the number of indices to be selected.", | ||
| :start_idx => "`start_idx::Int64`. the starting location in `idx` where the indices | ||
| will be placed.", | ||
| :A => "`A::AbstractMatrix`, a target matrix for approximation.", | ||
| ) | ||
| select_output_list = Dict{Symbol,String}( | ||
| :selector_recipe => "A `SelectorRecipe` object.", | ||
| ) | ||
| select_method_description = Dict{Symbol,String}( | ||
| :update_selector => "A function that updates the `SelectorRecipe` in place given the | ||
| arguments.", | ||
| :complete_selector => "A function that generates a `SelectorRecipe` given | ||
| arguments.", | ||
| :select_indices => "A function that selects indices from a matrix `A` using a specific | ||
| `SelectorRecipe`. It updates the vector `idx` in place with `n_idx` new indices starting | ||
| at index `start_idx`." | ||
| ) | ||
| ################################## | ||
| # Complete Selector Interface | ||
| ################################## | ||
| """ | ||
| complete_selector(selector::Selector, A::AbstractMatrix) | ||
|
|
||
| $(select_method_description[:complete_selector]) | ||
|
|
||
| # Arguments | ||
| - $(select_arg_list[:selector]) | ||
| - $(select_arg_list[:A]) | ||
|
|
||
| # Outputs | ||
| - $(select_output_list[:selector_recipe]) | ||
| """ | ||
| function complete_selector(selector::Selector, A::AbstractMatrix) | ||
| return throw( | ||
| ArgumentError( | ||
| "No method `complete_selector` exists for selector of type\ | ||
| $(typeof(selector)) and matrix of type $(typeof(A))." | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| ################################## | ||
| # update_selector! | ||
| ################################## | ||
| """ | ||
| update_selector!(selector::SelectorRecipe) | ||
|
|
||
| $(select_method_description[:update_selector]) | ||
|
|
||
| # Arguments | ||
| - $(select_arg_list[:selector_recipe]) | ||
|
|
||
| # Outputs | ||
| - $(select_output_list[:selector_recipe]) | ||
| """ | ||
| function update_selector!(selector::SelectorRecipe) | ||
| return throw( | ||
| ArgumentError( | ||
| "No method `update_selector!` exists for selector of type\ | ||
| $(typeof(selector))." | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| """ | ||
| update_selector!(selector::SelectorRecipe, A::AbstractMatrix) | ||
|
|
||
| $(select_method_description[:update_selector]) | ||
|
|
||
| # Arguments | ||
| - $(select_arg_list[:selector_recipe]) | ||
| - $(select_arg_list[:A]) | ||
|
|
||
| # Outputs | ||
| - $(select_output_list[:selector_recipe]) | ||
| """ | ||
| function update_selector!(selector::SelectorRecipe, A::AbstractMatrix) | ||
| return update_selector!(selector) | ||
| end | ||
|
|
||
| #################################### | ||
| # select_indices! | ||
| #################################### | ||
| """ | ||
| select_indices!( | ||
| selector::SelectorRecipe, | ||
| A::AbstractMatrix, | ||
| idx::AbstractVector, | ||
|
Comment on lines
+111
to
+113
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. These need to be permuted to match the function |
||
| n_idx::Int64, | ||
| start_idx::Int64 | ||
| ) | ||
|
|
||
| $(select_method_description[:select_indices]) | ||
|
|
||
| # Arguments | ||
| - $(select_arg_list[:idx]) | ||
| - $(select_arg_list[:selector_recipe]) | ||
| - $(select_arg_list[:A]) | ||
| - $(select_arg_list[:n_idx]) | ||
| - $(select_arg_list[:start_idx]) | ||
|
|
||
| # Outputs | ||
| - Returns `nothing` | ||
| """ | ||
| function select_indices!( | ||
| idx::AbstractVector, | ||
| selector::SelectorRecipe, | ||
| A::AbstractMatrix, | ||
| n_idx::Int64, | ||
| start_idx::Int64 | ||
| ) | ||
| return throw( | ||
| ArgumentError( | ||
| "No method `select_indices` exists for selector of type $(typeof(selector)),\ | ||
| matrix of type $(typeof(A)), idx of type $(typeof(idx)), n_idx of type \ | ||
| $(typeof(n_idx)), and start_idx of type $(typeof(start_idx))." | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| # Include the selector files | ||
| include("Selectors/lupp.jl") | ||
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| LUPP <: Selector | ||
|
|
||
| A `Selector` that implements LU with partial pivoting for selecting column indices from a | ||
| matrix. | ||
|
|
||
| # Fields | ||
| - `compressor::Compressor`, the compression technique that will be applied to the matrix, | ||
| before selecting indices. | ||
|
|
||
| # Constructor | ||
| LUPP(;compressor = Identity()) | ||
|
|
||
| ## Keywords | ||
| - `compressor::Compressor`, the compression technique that will be applied to the matrix, | ||
| before selecting indices. Defaults to the `Identity` compressor. | ||
|
|
||
| ## Returns | ||
| - A `LUPP` object. | ||
|
|
||
| !!! note "Implementation Note" | ||
| LU with partial pivoting is classically implemented to select rows of a matrix. Here we | ||
| apply LU with partial pivoting to the transpose of the inputted matrix to select | ||
| columns. | ||
| """ | ||
| mutable struct LUPP <: Selector | ||
| compressor::Compressor | ||
| end | ||
|
|
||
| function LUPP(;compressor = Identity()) | ||
| return LUPP(compressor) | ||
| end | ||
|
|
||
| """ | ||
| LUPPRecipe <: SelectorRecipe | ||
|
|
||
| A `SelectorRecipe` that contains all the necessary preallocations for selecting column | ||
| indices from a matrix using LU with partial pivoting. | ||
|
|
||
nathanielpritchard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Fields | ||
| - `compressor::CompressorRecipe`, the compression technique that will applied to the matrix, | ||
| before selecting indices. | ||
| - `SA::AbstractMatrix`, a buffer matrix for storing the sketched matrix. | ||
|
|
||
| !!! note "Implementation Note" | ||
| LU with partial pivoting is classically implemented to select rows of a matrix. Here we | ||
| apply LU with partial pivoting to the transpose of the inputted matrix to select | ||
| columns. | ||
| """ | ||
| mutable struct LUPPRecipe <: SelectorRecipe | ||
| compressor::CompressorRecipe | ||
| SA::AbstractMatrix | ||
| end | ||
|
|
||
| function complete_selector(ingredients::LUPP, A::AbstractMatrix) | ||
| compressor = complete_compressor(ingredients.compressor, A) | ||
| n_rows, n_cols = size(compressor) | ||
| SA = Matrix{eltype(A)}(undef, n_rows, n_cols) | ||
| return LUPPRecipe(compressor, SA) | ||
| end | ||
|
|
||
| function update_selector!(selector::LUPPRecipe) | ||
| update_compressor!(selector.compressor) | ||
| return nothing | ||
| end | ||
|
|
||
| function select_indices!( | ||
| idx::AbstractVector, | ||
| selector::LUPPRecipe, | ||
| A::AbstractMatrix, | ||
| n_idx::Int64, | ||
| start_idx::Int64 | ||
| ) | ||
| # you cannot select more column indices than there are columns in the matrix | ||
| if n_idx > size(A, 2) | ||
| throw( | ||
| DimensionMismatch( | ||
| "`n_idx` cannot be larger than the number of columns in `A`." | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| # start_idx + n_idx must be less than the length of the idx vector | ||
| if start_idx + n_idx - 1 > size(idx, 1) | ||
| throw( | ||
| DimensionMismatch( | ||
| "`start_idx` + `n_idx` - 1 cannot be larger than the lenght of `idx`." | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| # you cannot select more indices than the compression dimension because that is when | ||
| # LUPP will stop selecting new pivots because the LU factorization will have been formed | ||
| if n_idx > selector.compressor.n_rows | ||
| throw( | ||
| DimensionMismatch( | ||
| "Must select fewer indices then the `compression_dim`." | ||
| ) | ||
| ) | ||
|
|
||
| end | ||
|
|
||
| mul!(selector.SA, selector.compressor, A) | ||
| # because LUPP selects rows and selectors select columns we need to pivot on A' | ||
| p = lu!(selector.SA').p | ||
| # store n_idx indices in the appropriate part of the idx | ||
| idx[start_idx:start_idx + n_idx - 1] = p[1:n_idx] | ||
| return nothing | ||
| end | ||
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.
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.
Need to either update docstring list or arguments (I think docstring list is easier) to make SelectorRecipe consistent