-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add sum_to_zero_matrix type #1506
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1a7cf3
Add sum_to_zero_matrix type
WardBrian 3798168
Also expose additional overloads for sum_to_zero_* functions
WardBrian 2356000
Deduplicate resizing logic in Ast_to_Mir
WardBrian 72b6c68
Don't really need a GADT for that
WardBrian a7d6382
Fix comment
WardBrian 66ab723
Rename helper function
WardBrian 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,41 +299,61 @@ | |
| |TupleTransformation _ | StochasticRow | StochasticColumn -> | ||
| [] | ||
|
|
||
| let rec param_size transform sizedtype = | ||
| let rec shrink_eigen f st = | ||
| match st with | ||
| | SizedType.SArray (t, d) -> SizedType.SArray (shrink_eigen f t, d) | ||
| | SVector (mem_pattern, d) | SMatrix (mem_pattern, d, _) -> | ||
| SVector (mem_pattern, f d) | ||
| | SInt | SReal | SComplex | SRowVector _ | STuple _ | SComplexRowVector _ | ||
| |SComplexVector _ | SComplexMatrix _ -> | ||
| (** Allows [shrink_helper] to operate either on each dimension independently | ||
| or on both matrix dimensions at once *) | ||
| type size_change = | ||
| | Univariate of (Expr.Typed.t -> Expr.Typed.t) | ||
| | Multivariate of (Expr.Typed.t -> Expr.Typed.t -> Expr.Typed.t) | ||
|
|
||
| (** We need to compute somewhat arbitrary new sizes for the unconstrained | ||
| parameters. This function handles the primary cases: | ||
|
|
||
| - A vector of size N is transformed to a vector of size (f N) | ||
| - A matrix of size N x M is transformed to a vector of size (f N) x (f_d2 M) | ||
| - A matrix of size N x N is transformed to a vector of size (f N M) | ||
| - Arrays of the above are handled recursively | ||
| *) | ||
| let rec shrink_helper (f : size_change) f_d2 st = | ||
| let f_assert_univariate d = | ||
| match f with | ||
| | Univariate f -> f d | ||
| | Multivariate _ -> | ||
| Common.ICE.internal_compiler_error | ||
| [%message | ||
| "Expecting SVector or SMatrix, got " (st : Expr.Typed.t SizedType.t)] | ||
| in | ||
| let rec shrink_eigen_mat f st = | ||
| match st with | ||
| | SizedType.SArray (t, d) -> SizedType.SArray (shrink_eigen_mat f t, d) | ||
| | SMatrix (mem_pattern, d1, d2) -> SVector (mem_pattern, f d1 d2) | ||
| | SInt | SReal | SComplex | SRowVector _ | SVector _ | STuple _ | ||
| |SComplexRowVector _ | SComplexVector _ | SComplexMatrix _ -> | ||
| Common.ICE.internal_compiler_error | ||
| [%message "Expecting SMatrix, got " (st : Expr.Typed.t SizedType.t)] | ||
| in | ||
| "To shrink a vector, the first argument must be a univariate \ | ||
| function " | ||
| (st : Expr.Typed.t SizedType.t)] in | ||
| match st with | ||
| | SizedType.SArray (t, d) -> SizedType.SArray (shrink_helper f f_d2 t, d) | ||
| | SVector (mem_pattern, d) -> SVector (mem_pattern, f_assert_univariate d) | ||
| | SRowVector (mem_pattern, d) -> | ||
| SRowVector (mem_pattern, f_assert_univariate d) | ||
| | SMatrix (mem_pattern, d1, d2) -> ( | ||
| match f with | ||
| | Univariate f_d1 -> SMatrix (mem_pattern, f_d1 d1, f_d2 d2) | ||
| | Multivariate f -> SVector (mem_pattern, f d1 d2)) | ||
| | SInt | SReal | SComplex | STuple _ | SComplexRowVector _ | ||
| |SComplexVector _ | SComplexMatrix _ -> | ||
| Common.ICE.internal_compiler_error | ||
| [%message | ||
| "Expecting SVector or SMatrix, got " (st : Expr.Typed.t SizedType.t)] | ||
|
|
||
| let rec param_size transform sizedtype = | ||
|
Contributor
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. (minor) can we change the name to |
||
| (* Functions for computing the new sizetype after some transformation *) | ||
| let shrink_eigen_mat f st = | ||
| (* Matrices become vectors, with size computed by [f] *) | ||
| shrink_helper (Multivariate f) Fn.id st in | ||
| let shrink_eigen_vec f st = | ||
| (* Matrices are mapped to vectors, only depending on their first dimension for sizing *) | ||
| shrink_eigen_mat (fun x _ -> f x) st in | ||
| let shrink_eigen f1 f2 st = | ||
| (* Types don't change, just sizes *) | ||
| shrink_helper (Univariate f1) f2 st in | ||
| (* Helper functions for computing the new sizes *) | ||
| let minus_one d = Expr.Helpers.(binop d Minus (int 1)) in | ||
| let k_choose_2 k = | ||
| Expr.Helpers.(binop (binop k Times (binop k Minus (int 1))) Divide (int 2)) | ||
| in | ||
| let rec stoch_size f1 f2 st = | ||
| match st with | ||
| | SizedType.SMatrix (mem_pattern, d1, d2) -> | ||
| SizedType.SMatrix (mem_pattern, f1 d1, f2 d2) | ||
| | SArray (t, d) -> SizedType.SArray (stoch_size f1 f2 t, d) | ||
| | SInt | SReal | SComplex | SRowVector _ | SVector _ | STuple _ | ||
| |SComplexRowVector _ | SComplexVector _ | SComplexMatrix _ -> | ||
| Common.ICE.internal_compiler_error | ||
| [%message "Expecting SMatrix, got " (st : Expr.Typed.t SizedType.t)] | ||
| in | ||
| let min_one d = Expr.Helpers.(binop d Minus (int 1)) in | ||
| match transform with | ||
| | Transformation.Identity | Lower _ | Upper _ | ||
| |LowerUpper (_, _) | ||
|
|
@@ -349,13 +369,16 @@ | |
| (SizedType.STuple | ||
| (List.map subtypes_transforms ~f:(fun (st, trans) -> | ||
| param_size trans st))) | ||
| | Simplex | SumToZero -> | ||
| shrink_eigen (fun d -> Expr.Helpers.(binop d Minus (int 1))) sizedtype | ||
| | CholeskyCorr | Correlation -> shrink_eigen k_choose_2 sizedtype | ||
| | StochasticRow -> stoch_size Fn.id min_one sizedtype | ||
| | StochasticColumn -> stoch_size min_one Fn.id sizedtype | ||
| | SumToZero -> shrink_eigen minus_one minus_one sizedtype | ||
| | Simplex | StochasticColumn -> shrink_eigen minus_one Fn.id sizedtype | ||
| | StochasticRow -> shrink_eigen Fn.id minus_one sizedtype | ||
| | CholeskyCorr | Correlation -> shrink_eigen_vec k_choose_2 sizedtype | ||
| | Covariance -> | ||
| shrink_eigen_vec | ||
| (fun k -> Expr.Helpers.(binop k Plus (k_choose_2 k))) | ||
| sizedtype | ||
| | CholeskyCov -> | ||
| (* (N * (N + 1)) / 2 + (M - N) * N *) | ||
| (* choose(N, 2) + (M - N) * N *) | ||
| shrink_eigen_mat | ||
| (fun m n -> | ||
| Expr.Helpers.( | ||
|
|
@@ -364,10 +387,6 @@ | |
| Plus | ||
| (binop (binop m Minus n) Times n))) | ||
| sizedtype | ||
| | Covariance -> | ||
| shrink_eigen | ||
| (fun k -> Expr.Helpers.(binop k Plus (k_choose_2 k))) | ||
| sizedtype | ||
|
|
||
| let rec check_decl var decl_type' decl_trans smeta adlevel = | ||
| let check_tuple var trans_subtypes = | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.