How do I allocate an array of vectors? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm trying to follow the code posted by a user on Stack Exchange and I'm having trouble with an allocation of a matrix. The code is:



dim = 3;
XX = Table[X[[i]], i, dim];


and it's supposed to create an array of:



X[[1]], X[[2]], X[[3]]


And it does, but I get the following warnings:



Part::partd: Part specification X[[1]] is longer than depth of object. >>
Part::partd: Part specification X[[2]] is longer than depth of object. >>
Part::partd: Part specification X[[3]] is longer than depth of object. >>
General::stop: Further output of Part::partd will be suppressed during this calculation. >>


What's the proper way to allocate this matrix without the warnings?










share|improve this question













closed as unclear what you're asking by Szabolcs, José Antonio Díaz Navas, bbgodfrey, Bob Hanlon, gwr Sep 11 at 10:55


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    If X has no parts, you can not use Part on it.
    – Î‘λέξανδρος Ζεγγ
    Sep 5 at 2:55






  • 1




    In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
    – Szabolcs
    Sep 5 at 7:57














up vote
1
down vote

favorite












I'm trying to follow the code posted by a user on Stack Exchange and I'm having trouble with an allocation of a matrix. The code is:



dim = 3;
XX = Table[X[[i]], i, dim];


and it's supposed to create an array of:



X[[1]], X[[2]], X[[3]]


And it does, but I get the following warnings:



Part::partd: Part specification X[[1]] is longer than depth of object. >>
Part::partd: Part specification X[[2]] is longer than depth of object. >>
Part::partd: Part specification X[[3]] is longer than depth of object. >>
General::stop: Further output of Part::partd will be suppressed during this calculation. >>


What's the proper way to allocate this matrix without the warnings?










share|improve this question













closed as unclear what you're asking by Szabolcs, José Antonio Díaz Navas, bbgodfrey, Bob Hanlon, gwr Sep 11 at 10:55


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    If X has no parts, you can not use Part on it.
    – Î‘λέξανδρος Ζεγγ
    Sep 5 at 2:55






  • 1




    In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
    – Szabolcs
    Sep 5 at 7:57












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to follow the code posted by a user on Stack Exchange and I'm having trouble with an allocation of a matrix. The code is:



dim = 3;
XX = Table[X[[i]], i, dim];


and it's supposed to create an array of:



X[[1]], X[[2]], X[[3]]


And it does, but I get the following warnings:



Part::partd: Part specification X[[1]] is longer than depth of object. >>
Part::partd: Part specification X[[2]] is longer than depth of object. >>
Part::partd: Part specification X[[3]] is longer than depth of object. >>
General::stop: Further output of Part::partd will be suppressed during this calculation. >>


What's the proper way to allocate this matrix without the warnings?










share|improve this question













I'm trying to follow the code posted by a user on Stack Exchange and I'm having trouble with an allocation of a matrix. The code is:



dim = 3;
XX = Table[X[[i]], i, dim];


and it's supposed to create an array of:



X[[1]], X[[2]], X[[3]]


And it does, but I get the following warnings:



Part::partd: Part specification X[[1]] is longer than depth of object. >>
Part::partd: Part specification X[[2]] is longer than depth of object. >>
Part::partd: Part specification X[[3]] is longer than depth of object. >>
General::stop: Further output of Part::partd will be suppressed during this calculation. >>


What's the proper way to allocate this matrix without the warnings?







matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 5 at 0:26









Donald Airey

1677




1677




closed as unclear what you're asking by Szabolcs, José Antonio Díaz Navas, bbgodfrey, Bob Hanlon, gwr Sep 11 at 10:55


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Szabolcs, José Antonio Díaz Navas, bbgodfrey, Bob Hanlon, gwr Sep 11 at 10:55


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    If X has no parts, you can not use Part on it.
    – Î‘λέξανδρος Ζεγγ
    Sep 5 at 2:55






  • 1




    In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
    – Szabolcs
    Sep 5 at 7:57












  • 1




    If X has no parts, you can not use Part on it.
    – Î‘λέξανδρος Ζεγγ
    Sep 5 at 2:55






  • 1




    In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
    – Szabolcs
    Sep 5 at 7:57







1




1




If X has no parts, you can not use Part on it.
– Î‘λέξανδρος Ζεγγ
Sep 5 at 2:55




If X has no parts, you can not use Part on it.
– Î‘λέξανδρος Ζεγγ
Sep 5 at 2:55




1




1




In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
– Szabolcs
Sep 5 at 7:57




In Mathematica there is almost never a need to pre-allocate an array. What are you actually trying to do?
– Szabolcs
Sep 5 at 7:57










2 Answers
2






active

oldest

votes

















up vote
6
down vote













Not sure for what you are going to use this for, but I use things like XX = Table[X[[i]], i, dim]; frequently for symbolic preprocessing code for Compile. In that case, Array[X, dim] won't work.



You can ignore the warning with XX = Quiet[Table[X[[i]], i, dim]]; or you can use XX = Table[Indexed[X,i], i, dim]; instead.



As I found out only recently, Compile is clever enough to substitute Indexed by Part as can be seen in the following example:



dim = 3;
XX = Table[Indexed[X, i], i, dim];
cDf = With[code = D[Sin[XX[[1]]] + XX[[2]]^3, XX, 1],
Compile[X, _Real, 1, code, CompilationTarget -> "C"]
];
CompiledFunctionTools`CompilePrint[cDf]





share|improve this answer




















  • That's a useful trick (the Indexed). I never knew that one.
    – b3m2a1
    Sep 5 at 6:55











  • @b3m2a1 Yeah, me neither. =D
    – Henrik Schumacher
    Sep 5 at 9:29


















up vote
2
down vote













How about this



dim = 3;
Array[X, dim]





share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote













    Not sure for what you are going to use this for, but I use things like XX = Table[X[[i]], i, dim]; frequently for symbolic preprocessing code for Compile. In that case, Array[X, dim] won't work.



    You can ignore the warning with XX = Quiet[Table[X[[i]], i, dim]]; or you can use XX = Table[Indexed[X,i], i, dim]; instead.



    As I found out only recently, Compile is clever enough to substitute Indexed by Part as can be seen in the following example:



    dim = 3;
    XX = Table[Indexed[X, i], i, dim];
    cDf = With[code = D[Sin[XX[[1]]] + XX[[2]]^3, XX, 1],
    Compile[X, _Real, 1, code, CompilationTarget -> "C"]
    ];
    CompiledFunctionTools`CompilePrint[cDf]





    share|improve this answer




















    • That's a useful trick (the Indexed). I never knew that one.
      – b3m2a1
      Sep 5 at 6:55











    • @b3m2a1 Yeah, me neither. =D
      – Henrik Schumacher
      Sep 5 at 9:29















    up vote
    6
    down vote













    Not sure for what you are going to use this for, but I use things like XX = Table[X[[i]], i, dim]; frequently for symbolic preprocessing code for Compile. In that case, Array[X, dim] won't work.



    You can ignore the warning with XX = Quiet[Table[X[[i]], i, dim]]; or you can use XX = Table[Indexed[X,i], i, dim]; instead.



    As I found out only recently, Compile is clever enough to substitute Indexed by Part as can be seen in the following example:



    dim = 3;
    XX = Table[Indexed[X, i], i, dim];
    cDf = With[code = D[Sin[XX[[1]]] + XX[[2]]^3, XX, 1],
    Compile[X, _Real, 1, code, CompilationTarget -> "C"]
    ];
    CompiledFunctionTools`CompilePrint[cDf]





    share|improve this answer




















    • That's a useful trick (the Indexed). I never knew that one.
      – b3m2a1
      Sep 5 at 6:55











    • @b3m2a1 Yeah, me neither. =D
      – Henrik Schumacher
      Sep 5 at 9:29













    up vote
    6
    down vote










    up vote
    6
    down vote









    Not sure for what you are going to use this for, but I use things like XX = Table[X[[i]], i, dim]; frequently for symbolic preprocessing code for Compile. In that case, Array[X, dim] won't work.



    You can ignore the warning with XX = Quiet[Table[X[[i]], i, dim]]; or you can use XX = Table[Indexed[X,i], i, dim]; instead.



    As I found out only recently, Compile is clever enough to substitute Indexed by Part as can be seen in the following example:



    dim = 3;
    XX = Table[Indexed[X, i], i, dim];
    cDf = With[code = D[Sin[XX[[1]]] + XX[[2]]^3, XX, 1],
    Compile[X, _Real, 1, code, CompilationTarget -> "C"]
    ];
    CompiledFunctionTools`CompilePrint[cDf]





    share|improve this answer












    Not sure for what you are going to use this for, but I use things like XX = Table[X[[i]], i, dim]; frequently for symbolic preprocessing code for Compile. In that case, Array[X, dim] won't work.



    You can ignore the warning with XX = Quiet[Table[X[[i]], i, dim]]; or you can use XX = Table[Indexed[X,i], i, dim]; instead.



    As I found out only recently, Compile is clever enough to substitute Indexed by Part as can be seen in the following example:



    dim = 3;
    XX = Table[Indexed[X, i], i, dim];
    cDf = With[code = D[Sin[XX[[1]]] + XX[[2]]^3, XX, 1],
    Compile[X, _Real, 1, code, CompilationTarget -> "C"]
    ];
    CompiledFunctionTools`CompilePrint[cDf]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 5 at 5:26









    Henrik Schumacher

    38.5k253114




    38.5k253114











    • That's a useful trick (the Indexed). I never knew that one.
      – b3m2a1
      Sep 5 at 6:55











    • @b3m2a1 Yeah, me neither. =D
      – Henrik Schumacher
      Sep 5 at 9:29

















    • That's a useful trick (the Indexed). I never knew that one.
      – b3m2a1
      Sep 5 at 6:55











    • @b3m2a1 Yeah, me neither. =D
      – Henrik Schumacher
      Sep 5 at 9:29
















    That's a useful trick (the Indexed). I never knew that one.
    – b3m2a1
    Sep 5 at 6:55





    That's a useful trick (the Indexed). I never knew that one.
    – b3m2a1
    Sep 5 at 6:55













    @b3m2a1 Yeah, me neither. =D
    – Henrik Schumacher
    Sep 5 at 9:29





    @b3m2a1 Yeah, me neither. =D
    – Henrik Schumacher
    Sep 5 at 9:29











    up vote
    2
    down vote













    How about this



    dim = 3;
    Array[X, dim]





    share|improve this answer
























      up vote
      2
      down vote













      How about this



      dim = 3;
      Array[X, dim]





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        How about this



        dim = 3;
        Array[X, dim]





        share|improve this answer












        How about this



        dim = 3;
        Array[X, dim]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 5 at 2:57









        Αλέξανδρος Ζεγγ

        1,852721




        1,852721












            這個網誌中的熱門文章

            How to combine Bézier curves to a surface?

            Mutual Information Always Non-negative

            Why am i infinitely getting the same tweet with the Twitter Search API?