Julia - console behaving differently than include(“myfile.jl”)











up vote
1
down vote

favorite












I would like to execute the following code, which works perfectly well when I type every line into my Julia console on Windows 10, but throws an error because of the mismatching type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} (my subsequent code expects Array{Float64,2}).



This is the code:



x = [0.2, 0.1, 0.2]
y = [-0.5 0.0 0.5]

fx = x * y
fy = fx'

return fx::Array{Float64,2}, fy::Array{Float64,2}


There is a TypeError, because fy seems to be of type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} instead of Array{Float64,2}.



How can I do a transpose and get a "normal" Array{Float64,2} object ?



And why does this work when I type every line into my Julia console, but does not when I run the file via include("myfile.jl") ?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I would like to execute the following code, which works perfectly well when I type every line into my Julia console on Windows 10, but throws an error because of the mismatching type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} (my subsequent code expects Array{Float64,2}).



    This is the code:



    x = [0.2, 0.1, 0.2]
    y = [-0.5 0.0 0.5]

    fx = x * y
    fy = fx'

    return fx::Array{Float64,2}, fy::Array{Float64,2}


    There is a TypeError, because fy seems to be of type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} instead of Array{Float64,2}.



    How can I do a transpose and get a "normal" Array{Float64,2} object ?



    And why does this work when I type every line into my Julia console, but does not when I run the file via include("myfile.jl") ?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I would like to execute the following code, which works perfectly well when I type every line into my Julia console on Windows 10, but throws an error because of the mismatching type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} (my subsequent code expects Array{Float64,2}).



      This is the code:



      x = [0.2, 0.1, 0.2]
      y = [-0.5 0.0 0.5]

      fx = x * y
      fy = fx'

      return fx::Array{Float64,2}, fy::Array{Float64,2}


      There is a TypeError, because fy seems to be of type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} instead of Array{Float64,2}.



      How can I do a transpose and get a "normal" Array{Float64,2} object ?



      And why does this work when I type every line into my Julia console, but does not when I run the file via include("myfile.jl") ?










      share|improve this question













      I would like to execute the following code, which works perfectly well when I type every line into my Julia console on Windows 10, but throws an error because of the mismatching type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} (my subsequent code expects Array{Float64,2}).



      This is the code:



      x = [0.2, 0.1, 0.2]
      y = [-0.5 0.0 0.5]

      fx = x * y
      fy = fx'

      return fx::Array{Float64,2}, fy::Array{Float64,2}


      There is a TypeError, because fy seems to be of type LinearAlgebra.Adjoint{Float64,Array{Float64,2}} instead of Array{Float64,2}.



      How can I do a transpose and get a "normal" Array{Float64,2} object ?



      And why does this work when I type every line into my Julia console, but does not when I run the file via include("myfile.jl") ?







      julia-lang






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 14:35









      cookiedealer

      82212




      82212
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Use collect to have a copy of actual data rather than a transformed view of the original (note that this rule applies to many other similar situations):



          julia> x = [0.2, 0.1, 0.2];                         
          julia> y = [-0.5 0.0 0.5];

          julia> fx = x * y
          3×3 Array{Float64,2}:
          -0.1 0.0 0.1
          -0.05 0.0 0.05
          -0.1 0.0 0.1

          julia> fy = fx'
          3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
          -0.1 -0.05 -0.1
          0.0 0.0 0.0
          0.1 0.05 0.1

          julia> fy = collect(fx')
          3×3 Array{Float64,2}:
          -0.1 -0.05 -0.1
          0.0 0.0 0.0
          0.1 0.05 0.1





          share|improve this answer





















          • This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
            – Bogumił Kamiński
            Nov 10 at 15:31










          • Thank you, this was what I was looking for! :-)
            – cookiedealer
            Nov 10 at 15:35


















          up vote
          2
          down vote













          To get a normal Matrix{Float64} use:



          fy = permutedims(fx)


          or



          fy = Matrix(fx')


          Those two are not 100% equivalent in general as fx' is a recursive adjoint operation (conjugate transpose), while permutedims is a non-recursive transpose, but in your case they will give the same result.



          What does recursive adjoint mean exactly?




          • recursive: the conjugate transpose is applied recursively to all entries of the array (in your case you have array of numbers and transpose of a number is the same number so this does not change anything);

          • adjoint: if you would have complex numbers then the operation would return their complex conjugates (in your case you have real numbers so this does not change anything);


          Here is an example when both things matter:



          julia> x = [[im, -im], [1-im 1+im]]
          2-element Array{Array{Complex{Int64},N} where N,1}:
          [0+1im, 0-1im]
          [1-1im 1+1im]

          julia> permutedims(x)
          1×2 Array{Array{Complex{Int64},N} where N,2}:
          [0+1im, 0-1im] [1-1im 1+1im]

          julia> Matrix(x')
          1×2 Array{AbstractArray{Complex{Int64},N} where N,2}:
          [0-1im 0+1im] [1+1im; 1-1im]


          However, unless you really need to you do not have to do it if you really need to get a conjugate transpose of your data. It is enough to change type assertion to



          return fx::Array{Float64,2}, fy::AbstractArray{Float64,2}


          or



          return fx::Matrix{Float64}, fy::AbstractMatrix{Float64}


          Conjugate transpose was designed to avoid unnecessary allocation of data and most of the time this will be more efficient for you (especially with large matrices).



          Finally the line:



          return fx::Array{Float64,2}, fy::Array{Float64,2}


          throws an error also in the Julia command line (not only when run from a script).






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239982%2fjulia-console-behaving-differently-than-includemyfile-jl%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Use collect to have a copy of actual data rather than a transformed view of the original (note that this rule applies to many other similar situations):



            julia> x = [0.2, 0.1, 0.2];                         
            julia> y = [-0.5 0.0 0.5];

            julia> fx = x * y
            3×3 Array{Float64,2}:
            -0.1 0.0 0.1
            -0.05 0.0 0.05
            -0.1 0.0 0.1

            julia> fy = fx'
            3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1

            julia> fy = collect(fx')
            3×3 Array{Float64,2}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1





            share|improve this answer





















            • This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
              – Bogumił Kamiński
              Nov 10 at 15:31










            • Thank you, this was what I was looking for! :-)
              – cookiedealer
              Nov 10 at 15:35















            up vote
            2
            down vote



            accepted










            Use collect to have a copy of actual data rather than a transformed view of the original (note that this rule applies to many other similar situations):



            julia> x = [0.2, 0.1, 0.2];                         
            julia> y = [-0.5 0.0 0.5];

            julia> fx = x * y
            3×3 Array{Float64,2}:
            -0.1 0.0 0.1
            -0.05 0.0 0.05
            -0.1 0.0 0.1

            julia> fy = fx'
            3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1

            julia> fy = collect(fx')
            3×3 Array{Float64,2}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1





            share|improve this answer





















            • This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
              – Bogumił Kamiński
              Nov 10 at 15:31










            • Thank you, this was what I was looking for! :-)
              – cookiedealer
              Nov 10 at 15:35













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Use collect to have a copy of actual data rather than a transformed view of the original (note that this rule applies to many other similar situations):



            julia> x = [0.2, 0.1, 0.2];                         
            julia> y = [-0.5 0.0 0.5];

            julia> fx = x * y
            3×3 Array{Float64,2}:
            -0.1 0.0 0.1
            -0.05 0.0 0.05
            -0.1 0.0 0.1

            julia> fy = fx'
            3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1

            julia> fy = collect(fx')
            3×3 Array{Float64,2}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1





            share|improve this answer












            Use collect to have a copy of actual data rather than a transformed view of the original (note that this rule applies to many other similar situations):



            julia> x = [0.2, 0.1, 0.2];                         
            julia> y = [-0.5 0.0 0.5];

            julia> fx = x * y
            3×3 Array{Float64,2}:
            -0.1 0.0 0.1
            -0.05 0.0 0.05
            -0.1 0.0 0.1

            julia> fy = fx'
            3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1

            julia> fy = collect(fx')
            3×3 Array{Float64,2}:
            -0.1 -0.05 -0.1
            0.0 0.0 0.0
            0.1 0.05 0.1






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 15:15









            Przemyslaw Szufel

            1,119110




            1,119110












            • This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
              – Bogumił Kamiński
              Nov 10 at 15:31










            • Thank you, this was what I was looking for! :-)
              – cookiedealer
              Nov 10 at 15:35


















            • This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
              – Bogumił Kamiński
              Nov 10 at 15:31










            • Thank you, this was what I was looking for! :-)
              – cookiedealer
              Nov 10 at 15:35
















            This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
            – Bogumił Kamiński
            Nov 10 at 15:31




            This is a good solution, but one should make sure that fx' really gives them what they expected and if materializing fx' is really needed as I have outlined in my answer.
            – Bogumił Kamiński
            Nov 10 at 15:31












            Thank you, this was what I was looking for! :-)
            – cookiedealer
            Nov 10 at 15:35




            Thank you, this was what I was looking for! :-)
            – cookiedealer
            Nov 10 at 15:35












            up vote
            2
            down vote













            To get a normal Matrix{Float64} use:



            fy = permutedims(fx)


            or



            fy = Matrix(fx')


            Those two are not 100% equivalent in general as fx' is a recursive adjoint operation (conjugate transpose), while permutedims is a non-recursive transpose, but in your case they will give the same result.



            What does recursive adjoint mean exactly?




            • recursive: the conjugate transpose is applied recursively to all entries of the array (in your case you have array of numbers and transpose of a number is the same number so this does not change anything);

            • adjoint: if you would have complex numbers then the operation would return their complex conjugates (in your case you have real numbers so this does not change anything);


            Here is an example when both things matter:



            julia> x = [[im, -im], [1-im 1+im]]
            2-element Array{Array{Complex{Int64},N} where N,1}:
            [0+1im, 0-1im]
            [1-1im 1+1im]

            julia> permutedims(x)
            1×2 Array{Array{Complex{Int64},N} where N,2}:
            [0+1im, 0-1im] [1-1im 1+1im]

            julia> Matrix(x')
            1×2 Array{AbstractArray{Complex{Int64},N} where N,2}:
            [0-1im 0+1im] [1+1im; 1-1im]


            However, unless you really need to you do not have to do it if you really need to get a conjugate transpose of your data. It is enough to change type assertion to



            return fx::Array{Float64,2}, fy::AbstractArray{Float64,2}


            or



            return fx::Matrix{Float64}, fy::AbstractMatrix{Float64}


            Conjugate transpose was designed to avoid unnecessary allocation of data and most of the time this will be more efficient for you (especially with large matrices).



            Finally the line:



            return fx::Array{Float64,2}, fy::Array{Float64,2}


            throws an error also in the Julia command line (not only when run from a script).






            share|improve this answer



























              up vote
              2
              down vote













              To get a normal Matrix{Float64} use:



              fy = permutedims(fx)


              or



              fy = Matrix(fx')


              Those two are not 100% equivalent in general as fx' is a recursive adjoint operation (conjugate transpose), while permutedims is a non-recursive transpose, but in your case they will give the same result.



              What does recursive adjoint mean exactly?




              • recursive: the conjugate transpose is applied recursively to all entries of the array (in your case you have array of numbers and transpose of a number is the same number so this does not change anything);

              • adjoint: if you would have complex numbers then the operation would return their complex conjugates (in your case you have real numbers so this does not change anything);


              Here is an example when both things matter:



              julia> x = [[im, -im], [1-im 1+im]]
              2-element Array{Array{Complex{Int64},N} where N,1}:
              [0+1im, 0-1im]
              [1-1im 1+1im]

              julia> permutedims(x)
              1×2 Array{Array{Complex{Int64},N} where N,2}:
              [0+1im, 0-1im] [1-1im 1+1im]

              julia> Matrix(x')
              1×2 Array{AbstractArray{Complex{Int64},N} where N,2}:
              [0-1im 0+1im] [1+1im; 1-1im]


              However, unless you really need to you do not have to do it if you really need to get a conjugate transpose of your data. It is enough to change type assertion to



              return fx::Array{Float64,2}, fy::AbstractArray{Float64,2}


              or



              return fx::Matrix{Float64}, fy::AbstractMatrix{Float64}


              Conjugate transpose was designed to avoid unnecessary allocation of data and most of the time this will be more efficient for you (especially with large matrices).



              Finally the line:



              return fx::Array{Float64,2}, fy::Array{Float64,2}


              throws an error also in the Julia command line (not only when run from a script).






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                To get a normal Matrix{Float64} use:



                fy = permutedims(fx)


                or



                fy = Matrix(fx')


                Those two are not 100% equivalent in general as fx' is a recursive adjoint operation (conjugate transpose), while permutedims is a non-recursive transpose, but in your case they will give the same result.



                What does recursive adjoint mean exactly?




                • recursive: the conjugate transpose is applied recursively to all entries of the array (in your case you have array of numbers and transpose of a number is the same number so this does not change anything);

                • adjoint: if you would have complex numbers then the operation would return their complex conjugates (in your case you have real numbers so this does not change anything);


                Here is an example when both things matter:



                julia> x = [[im, -im], [1-im 1+im]]
                2-element Array{Array{Complex{Int64},N} where N,1}:
                [0+1im, 0-1im]
                [1-1im 1+1im]

                julia> permutedims(x)
                1×2 Array{Array{Complex{Int64},N} where N,2}:
                [0+1im, 0-1im] [1-1im 1+1im]

                julia> Matrix(x')
                1×2 Array{AbstractArray{Complex{Int64},N} where N,2}:
                [0-1im 0+1im] [1+1im; 1-1im]


                However, unless you really need to you do not have to do it if you really need to get a conjugate transpose of your data. It is enough to change type assertion to



                return fx::Array{Float64,2}, fy::AbstractArray{Float64,2}


                or



                return fx::Matrix{Float64}, fy::AbstractMatrix{Float64}


                Conjugate transpose was designed to avoid unnecessary allocation of data and most of the time this will be more efficient for you (especially with large matrices).



                Finally the line:



                return fx::Array{Float64,2}, fy::Array{Float64,2}


                throws an error also in the Julia command line (not only when run from a script).






                share|improve this answer














                To get a normal Matrix{Float64} use:



                fy = permutedims(fx)


                or



                fy = Matrix(fx')


                Those two are not 100% equivalent in general as fx' is a recursive adjoint operation (conjugate transpose), while permutedims is a non-recursive transpose, but in your case they will give the same result.



                What does recursive adjoint mean exactly?




                • recursive: the conjugate transpose is applied recursively to all entries of the array (in your case you have array of numbers and transpose of a number is the same number so this does not change anything);

                • adjoint: if you would have complex numbers then the operation would return their complex conjugates (in your case you have real numbers so this does not change anything);


                Here is an example when both things matter:



                julia> x = [[im, -im], [1-im 1+im]]
                2-element Array{Array{Complex{Int64},N} where N,1}:
                [0+1im, 0-1im]
                [1-1im 1+1im]

                julia> permutedims(x)
                1×2 Array{Array{Complex{Int64},N} where N,2}:
                [0+1im, 0-1im] [1-1im 1+1im]

                julia> Matrix(x')
                1×2 Array{AbstractArray{Complex{Int64},N} where N,2}:
                [0-1im 0+1im] [1+1im; 1-1im]


                However, unless you really need to you do not have to do it if you really need to get a conjugate transpose of your data. It is enough to change type assertion to



                return fx::Array{Float64,2}, fy::AbstractArray{Float64,2}


                or



                return fx::Matrix{Float64}, fy::AbstractMatrix{Float64}


                Conjugate transpose was designed to avoid unnecessary allocation of data and most of the time this will be more efficient for you (especially with large matrices).



                Finally the line:



                return fx::Array{Float64,2}, fy::Array{Float64,2}


                throws an error also in the Julia command line (not only when run from a script).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 15:21

























                answered Nov 10 at 15:16









                Bogumił Kamiński

                10.5k11019




                10.5k11019






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239982%2fjulia-console-behaving-differently-than-includemyfile-jl%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python