inexact error in Julia — matrix calculation












1















The error happens after else condition. I want to replace A's column by new one. The following is the datatype of variables.It seems that it is datatype issue.



A = [1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]
b = [20 24 16 20]'
c = [-1 -2 -3 -4 0 0 0]'
C = [1 5 6 4]'

## convert the datatype of original data
C = vec(C)
# initial basis matrix
B = A[:, C]
# initial basic solution
x = inv(B)*b
c_B = c[C]
# initial reduced costs
c_r = (c' - c_B'*inv(B)*A)'
# if basic matrix is not identity (e.g., take other variables other than slack
# variables as starting point), convert B=I and N=inv(B)
ind = eye(length(C))
j = 1
if B != ind
for i in length(c)
if i in C
A[:,i] = ind[:,j]
j += 1
else
A[:,i] = inv(B)*A[:,i]
end
end
end









share|improve this question




















  • 2





    please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

    – Przemyslaw Szufel
    Nov 14 '18 at 12:05













  • Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

    – daycaster
    Nov 14 '18 at 13:09


















1















The error happens after else condition. I want to replace A's column by new one. The following is the datatype of variables.It seems that it is datatype issue.



A = [1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]
b = [20 24 16 20]'
c = [-1 -2 -3 -4 0 0 0]'
C = [1 5 6 4]'

## convert the datatype of original data
C = vec(C)
# initial basis matrix
B = A[:, C]
# initial basic solution
x = inv(B)*b
c_B = c[C]
# initial reduced costs
c_r = (c' - c_B'*inv(B)*A)'
# if basic matrix is not identity (e.g., take other variables other than slack
# variables as starting point), convert B=I and N=inv(B)
ind = eye(length(C))
j = 1
if B != ind
for i in length(c)
if i in C
A[:,i] = ind[:,j]
j += 1
else
A[:,i] = inv(B)*A[:,i]
end
end
end









share|improve this question




















  • 2





    please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

    – Przemyslaw Szufel
    Nov 14 '18 at 12:05













  • Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

    – daycaster
    Nov 14 '18 at 13:09
















1












1








1








The error happens after else condition. I want to replace A's column by new one. The following is the datatype of variables.It seems that it is datatype issue.



A = [1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]
b = [20 24 16 20]'
c = [-1 -2 -3 -4 0 0 0]'
C = [1 5 6 4]'

## convert the datatype of original data
C = vec(C)
# initial basis matrix
B = A[:, C]
# initial basic solution
x = inv(B)*b
c_B = c[C]
# initial reduced costs
c_r = (c' - c_B'*inv(B)*A)'
# if basic matrix is not identity (e.g., take other variables other than slack
# variables as starting point), convert B=I and N=inv(B)
ind = eye(length(C))
j = 1
if B != ind
for i in length(c)
if i in C
A[:,i] = ind[:,j]
j += 1
else
A[:,i] = inv(B)*A[:,i]
end
end
end









share|improve this question
















The error happens after else condition. I want to replace A's column by new one. The following is the datatype of variables.It seems that it is datatype issue.



A = [1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]
b = [20 24 16 20]'
c = [-1 -2 -3 -4 0 0 0]'
C = [1 5 6 4]'

## convert the datatype of original data
C = vec(C)
# initial basis matrix
B = A[:, C]
# initial basic solution
x = inv(B)*b
c_B = c[C]
# initial reduced costs
c_r = (c' - c_B'*inv(B)*A)'
# if basic matrix is not identity (e.g., take other variables other than slack
# variables as starting point), convert B=I and N=inv(B)
ind = eye(length(C))
j = 1
if B != ind
for i in length(c)
if i in C
A[:,i] = ind[:,j]
j += 1
else
A[:,i] = inv(B)*A[:,i]
end
end
end






julia-lang






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 12:45







Jiayan Yang

















asked Nov 14 '18 at 11:54









Jiayan YangJiayan Yang

184




184








  • 2





    please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

    – Przemyslaw Szufel
    Nov 14 '18 at 12:05













  • Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

    – daycaster
    Nov 14 '18 at 13:09
















  • 2





    please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

    – Przemyslaw Szufel
    Nov 14 '18 at 12:05













  • Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

    – daycaster
    Nov 14 '18 at 13:09










2




2





please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

– Przemyslaw Szufel
Nov 14 '18 at 12:05







please edit your post and paste console text rather a screenshot image. The text will be formatted as code if preceded by four spaces. You can also use Ctrl+K for that

– Przemyslaw Szufel
Nov 14 '18 at 12:05















Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

– daycaster
Nov 14 '18 at 13:09







Note that eye() has been deprecated in favor of I and Matrix constructors in versions 0.7 and 1.0.

– daycaster
Nov 14 '18 at 13:09














1 Answer
1






active

oldest

votes


















4














You have the following problems the code. First A is Matrix{Int64} and it should be Matrix{Float64}. You can fix it by writing:



A = Float64[1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]


Second - you probably want index i to range from 1 to length(c) so you should write your loop as:



for i in 1:length(c)


Finally - your code will not work under Julia 1.0 not only because eye is not defined, but also because you update variable j inside a loop. I would recommend you to wrap the whole code in a function or write global j += 1 for it to work under Julia 1.0.



EDIT:



The problem - in a nutshell is the following:



julia> A = [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> A[1] = 0.5
ERROR: InexactError: Int64(Int64, 0.5)


Array A can hold only integers, so you cannot assign a float to it. By writing Float64 in front of array literal you force it to have another type of element, so this works:



julia> A = Float64[1,2,3]
3-element Array{Float64,1}:
1.0
2.0
3.0

julia> A[1] = 0.5
0.5

julia> A
3-element Array{Float64,1}:
0.5
2.0
3.0


In short Julia knows if your array holds integers or floats and checks it. Sometimes type promotion is allowed (e.g. you can assign an integer to an array of floats as this normally does not lead to the loss of precision) as is explained here https://docs.julialang.org/en/latest/manual/conversion-and-promotion/#Conversion-1.






share|improve this answer


























  • Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

    – Jiayan Yang
    Nov 14 '18 at 14:54











  • I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

    – Bogumił Kamiński
    Nov 14 '18 at 15:38











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',
autoActivateHeartbeat: false,
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%2f53299677%2finexact-error-in-julia-matrix-calculation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You have the following problems the code. First A is Matrix{Int64} and it should be Matrix{Float64}. You can fix it by writing:



A = Float64[1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]


Second - you probably want index i to range from 1 to length(c) so you should write your loop as:



for i in 1:length(c)


Finally - your code will not work under Julia 1.0 not only because eye is not defined, but also because you update variable j inside a loop. I would recommend you to wrap the whole code in a function or write global j += 1 for it to work under Julia 1.0.



EDIT:



The problem - in a nutshell is the following:



julia> A = [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> A[1] = 0.5
ERROR: InexactError: Int64(Int64, 0.5)


Array A can hold only integers, so you cannot assign a float to it. By writing Float64 in front of array literal you force it to have another type of element, so this works:



julia> A = Float64[1,2,3]
3-element Array{Float64,1}:
1.0
2.0
3.0

julia> A[1] = 0.5
0.5

julia> A
3-element Array{Float64,1}:
0.5
2.0
3.0


In short Julia knows if your array holds integers or floats and checks it. Sometimes type promotion is allowed (e.g. you can assign an integer to an array of floats as this normally does not lead to the loss of precision) as is explained here https://docs.julialang.org/en/latest/manual/conversion-and-promotion/#Conversion-1.






share|improve this answer


























  • Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

    – Jiayan Yang
    Nov 14 '18 at 14:54











  • I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

    – Bogumił Kamiński
    Nov 14 '18 at 15:38
















4














You have the following problems the code. First A is Matrix{Int64} and it should be Matrix{Float64}. You can fix it by writing:



A = Float64[1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]


Second - you probably want index i to range from 1 to length(c) so you should write your loop as:



for i in 1:length(c)


Finally - your code will not work under Julia 1.0 not only because eye is not defined, but also because you update variable j inside a loop. I would recommend you to wrap the whole code in a function or write global j += 1 for it to work under Julia 1.0.



EDIT:



The problem - in a nutshell is the following:



julia> A = [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> A[1] = 0.5
ERROR: InexactError: Int64(Int64, 0.5)


Array A can hold only integers, so you cannot assign a float to it. By writing Float64 in front of array literal you force it to have another type of element, so this works:



julia> A = Float64[1,2,3]
3-element Array{Float64,1}:
1.0
2.0
3.0

julia> A[1] = 0.5
0.5

julia> A
3-element Array{Float64,1}:
0.5
2.0
3.0


In short Julia knows if your array holds integers or floats and checks it. Sometimes type promotion is allowed (e.g. you can assign an integer to an array of floats as this normally does not lead to the loss of precision) as is explained here https://docs.julialang.org/en/latest/manual/conversion-and-promotion/#Conversion-1.






share|improve this answer


























  • Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

    – Jiayan Yang
    Nov 14 '18 at 14:54











  • I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

    – Bogumił Kamiński
    Nov 14 '18 at 15:38














4












4








4







You have the following problems the code. First A is Matrix{Int64} and it should be Matrix{Float64}. You can fix it by writing:



A = Float64[1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]


Second - you probably want index i to range from 1 to length(c) so you should write your loop as:



for i in 1:length(c)


Finally - your code will not work under Julia 1.0 not only because eye is not defined, but also because you update variable j inside a loop. I would recommend you to wrap the whole code in a function or write global j += 1 for it to work under Julia 1.0.



EDIT:



The problem - in a nutshell is the following:



julia> A = [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> A[1] = 0.5
ERROR: InexactError: Int64(Int64, 0.5)


Array A can hold only integers, so you cannot assign a float to it. By writing Float64 in front of array literal you force it to have another type of element, so this works:



julia> A = Float64[1,2,3]
3-element Array{Float64,1}:
1.0
2.0
3.0

julia> A[1] = 0.5
0.5

julia> A
3-element Array{Float64,1}:
0.5
2.0
3.0


In short Julia knows if your array holds integers or floats and checks it. Sometimes type promotion is allowed (e.g. you can assign an integer to an array of floats as this normally does not lead to the loss of precision) as is explained here https://docs.julialang.org/en/latest/manual/conversion-and-promotion/#Conversion-1.






share|improve this answer















You have the following problems the code. First A is Matrix{Int64} and it should be Matrix{Float64}. You can fix it by writing:



A = Float64[1 1 1 1 1 0 0;
1 2 0 3 0 1 0;
2 1 2 0 0 0 -1;
2 3 1 1 0 0 0]


Second - you probably want index i to range from 1 to length(c) so you should write your loop as:



for i in 1:length(c)


Finally - your code will not work under Julia 1.0 not only because eye is not defined, but also because you update variable j inside a loop. I would recommend you to wrap the whole code in a function or write global j += 1 for it to work under Julia 1.0.



EDIT:



The problem - in a nutshell is the following:



julia> A = [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> A[1] = 0.5
ERROR: InexactError: Int64(Int64, 0.5)


Array A can hold only integers, so you cannot assign a float to it. By writing Float64 in front of array literal you force it to have another type of element, so this works:



julia> A = Float64[1,2,3]
3-element Array{Float64,1}:
1.0
2.0
3.0

julia> A[1] = 0.5
0.5

julia> A
3-element Array{Float64,1}:
0.5
2.0
3.0


In short Julia knows if your array holds integers or floats and checks it. Sometimes type promotion is allowed (e.g. you can assign an integer to an array of floats as this normally does not lead to the loss of precision) as is explained here https://docs.julialang.org/en/latest/manual/conversion-and-promotion/#Conversion-1.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 15:37

























answered Nov 14 '18 at 13:52









Bogumił KamińskiBogumił Kamiński

13k11220




13k11220













  • Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

    – Jiayan Yang
    Nov 14 '18 at 14:54











  • I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

    – Bogumił Kamiński
    Nov 14 '18 at 15:38



















  • Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

    – Jiayan Yang
    Nov 14 '18 at 14:54











  • I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

    – Bogumił Kamiński
    Nov 14 '18 at 15:38

















Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

– Jiayan Yang
Nov 14 '18 at 14:54





Hi, thank you for your solution. The function eye works well in Julia on my laptop though I changes it to another. However the problem has been solved I still don't understand why it affects A[:,i] = inv(B)*A[:,i].

– Jiayan Yang
Nov 14 '18 at 14:54













I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

– Bogumił Kamiński
Nov 14 '18 at 15:38





I have added an explanation. I would highly recommend you to read the Julia manual as it contains many useful examples and explanations.

– Bogumił Kamiński
Nov 14 '18 at 15:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299677%2finexact-error-in-julia-matrix-calculation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine