Skip to content
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

How to construct a DMatrix from a slice of slices ? #873

Closed
pwnorbitals opened this issue Apr 20, 2021 · 4 comments · Fixed by #886
Closed

How to construct a DMatrix from a slice of slices ? #873

pwnorbitals opened this issue Apr 20, 2021 · 4 comments · Fixed by #886

Comments

@pwnorbitals
Copy link

pwnorbitals commented Apr 20, 2021

I'd like to construct a DMatrix (or a Matrix which size is auto-deduced) with the following syntax. The reason I'd like the matrix size to be auto-deduced is that I'm lazy and don't want to count the size (can be very large numbers), and the reason I'd like to use this syntax is because it's (I think) easy to deduce the matrix size from it, and would be close to being to compatible with Numpy. Of course it can be a variation of this syntax.

Does this exist already ? If not, are there plans to implement it ?

Thank you for your consideration

PS : bonus points if you find the algorithm this matrix is from ;)

Code
let a = DMatrix::from_something([
    [
        1.0 / 18.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        1.0 / 48.0,
        1.0 / 16.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        1.0 / 32.0,
        0.0,
        3.0 / 32.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        5.0 / 16.0,
        0.0,
        -75.0 / 64.0,
        75.0 / 64.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        3.0 / 80.0,
        0.0,
        0.0,
        3.0 / 16.0,
        3.0 / 20.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        29443841.0 / 614563906.0,
        0.0,
        0.0,
        77736538.0 / 692538347.0,
        -28693883.0 / 1125000000.0,
        23124283.0 / 1800000000.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        16016141.0 / 946692911.0,
        0.0,
        0.0,
        61564180.0 / 158732637.0,
        22789713.0 / 633445777.0,
        545815736.0 / 2771057229.0,
        -180193667.0 / 1043307555.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        39632708.0 / 573591083.0,
        0.0,
        0.0,
        -433636366.0 / 683701615.0,
        -421739975.0 / 2616292301.0,
        100302831.0 / 723423059.0,
        790204164.0 / 839813087.0,
        800635310.0 / 3783071287.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        246121993.0 / 1340847787.0,
        0.0,
        0.0,
        -37695042795.0 / 15268766246.0,
        -309121744.0 / 1061227803.0,
        -12992083.0 / 490766935.0,
        6005943493.0 / 2108947869.0,
        393006217.0 / 1396673457.0,
        123872331.0 / 1001029789.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        -1028468189.0 / 846180014.0,
        0.0,
        0.0,
        8478235783.0 / 508512852.0,
        1311729495.0 / 1432422823.0,
        -10304129995.0 / 1701304382.0,
        -48777925059.0 / 3047939560.0,
        15336726248.0 / 1032824649.0,
        -45442868181.0 / 3398467696.0,
        3065993473.0 / 597172653.0,
        0.0,
        0.0,
        0.0,
    ],
    [
        185892177.0 / 718116043.0,
        0.0,
        0.0,
        -3185094517.0 / 667107341.0,
        -477755414.0 / 1098053517.0,
        -703635378.0 / 230739211.0,
        5731566787.0 / 1027545527.0,
        5232866602.0 / 850066563.0,
        -4093664535.0 / 808688257.0,
        3962137247.0 / 1805957418.0,
        65686358.0 / 487910083.0,
        0.0,
        0.0,
    ],
    [
        403863854.0 / 491063109.0,
        0.0,
        0.0,
        -5068492393.0 / 434740067.0,
        -411421997.0 / 543043805.0,
        652783627.0 / 914296604.0,
        11173962825.0 / 925320556.0,
        -13158990841.0 / 6184727034.0,
        3936647629.0 / 1978049680.0,
        -160528059.0 / 685178525.0,
        248638103.0 / 1413531060.0,
        0.0,
        0.0,
    ],
]);
@Andlon
Copy link
Sponsor Collaborator

Andlon commented Apr 20, 2021

Hi @pwnorbitals, I actually happen to be working on a procedural macro with the following syntax (MATLAB-like):

// Statically allocated 2x3 matrix (SMatrix<_, 2, 3>)
let a = matrix![1, 2, 3;
                4, 5, 6];
                      
// Same, but DMatrix instead
let b = dmatrix![1, 2, 3;
                 4, 5, 6];

There will also be complementary vector! and dvector! macros.

Hopefully that suits your requirements?

The main work is done, I'm just polishing, writing tests and documenting. However, I'll be quite busy over the next weeks so might still take some time until I can land the PR.

@pwnorbitals
Copy link
Author

pwnorbitals commented Apr 20, 2021

Yes, that looks great ! Looking forwards to it, thank you very much for your answer. Is there anything one can do/contribute to help speed-up the process ?

@Andlon
Copy link
Sponsor Collaborator

Andlon commented Apr 21, 2021

I think at the moment you might just have to be a little patient! Perhaps I can manage to put up a "work in progress" PR sooner rather than later though, in which case a review and/or feedback might be helpful

@Andlon Andlon mentioned this issue May 3, 2021
@Andlon
Copy link
Sponsor Collaborator

Andlon commented May 5, 2021

@pwnorbitals: since you expressed desire to help with this, the functionality is now under review in #886. If you have any concerns with the current implementation you're welcome to address them there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants