ERROR: BoundsError: attempt to access Tuple{Int64} at index [2]
Stacktrace:
[1] indexed_iterate
# .\tuple.jl:89 [inlined]
When I run my Julia code, it shows this BoundsError. What does this mean and how can I solve it?
My code is to find the size of a Vector{Float64}.
(m,n)=size(c)
where c is a Vector{Float64}.
A Tuple{Int64} is a tuple containing a single element. So the error message is saying that you tried to access element 2 of a one-element tuple.
In Julia, vectors are truly one-dimensional, so size returns only one value (for eg. size(1:10) is just (10,)). When you try to assign that to (m, n), you're trying to extract two values from it, hence the error.
Since you know it's a Vector already, you know that its number of columns is going to be just 1. So you can instead just do n = length(c) to obtain the number of elements c contains.
Related
I've been writing a computational fluid dynamics code in the past two years. In my serial code I defined a derived type as follows:
type ptr_SCL
real(8), pointer :: Q
end type ptr_SCL
type arr_nod
real(8) :: Q
type(ptr_SCL)::p0(5)
type(ptr_SCL)::pj(5,5)
end type arr_nod
type(arr_nod ),target,allocatable :: Ucv(:) , Ustcv(:)
The reason behind this type of declaration is that every computational equation is written for a computational cell which has four neighbors and each cell has four neighbors for itself. This way i do not need to define in each iteration step where to read values from or write results to. Either way I should have defined a 3rd rank array to store number of neighbors places, I thought this may speed up my code.
----------
-------+--
------+*+-
-------+--
----------
(suppose 10 cells in each row)
for instance cell number 28 has 18,29,38 and 27 as its neighbors.
in each iteration step before looping over all cells, the results of previous step should be stored in an array like ustcv and after solving equations the two arrays are compared to measure convergence. because both arrays are of the same type I just used
ustcv=ucv
and it worked for me. but if I used a do loop for just main cell value updates I get error in my results.
DO K=1,kmx
ustcv(k)%q=ucv(k)%q
END DO
where there should be no errors, because I'm just changing all actual parts of memory targeted by pointers. but i don't know why this happens
Due to the need for speeding up my computations and for further developments, I transformed my serial code into parallel using co-arrays. but I didn't get proper results. I checked all parts of my code and found that there is issue with the above mentioned code. because co-array variable are not allowed to have pointer attribute. I defined a target co-array for each, called uco and ustco and pointed ucv and ustcv to them which are not co-arrays and are local to their images. and I do calculations on ustcv and ucv. I tried using both (1) and (2) get erroneous results.
What I explained above is a brief form of what really is going on in my complicated CFD code.
Is there a way I can use a command that selects a matrix to use based on a variable?
Need in this /
:If (way to select a matrix based on what variable L equals) (E,F)=1:Output E,F,"O
I don't want to make a specific go-to for every single matrix I need.
This is for creating maps with the matrix in case anyone has a better way.
If i understand correctly you want to get the value from a certain matrix, chosen dynamically depending on the value of a variable. You can kinda do this by putting the names of the matrices into a string, then grab a substring of the string, using sub(, at a dynamic offset, based on L, and then feeding that string into expr( to get a reference to the matrix, ie
:"[A][B][C]"->Str1, sub(Str1,2,1) yields "[B]", expr("[B]") yields Matrix B...so 2 maps to [B]. TI considers the symbol [A] (and all the other matrix vars) to be a single character, so "[A][B][C]" is a 3 character string.
Note that all of the matrix vars need to be input from the MATRIX menu (including inside the string). Typing in the individual [ A ] chracters will not work.
Also note you can't grab indexes off of a matrix returned with expr (ie expr("[A]")(1,2) so you need an extra matrix (I used [J]) to store the result.
For example
:"MAKE SOME MATRICES"
:[[1,2][3,4]]->[A]
:[[5,6][7,8]]->[B]
:[[9,10],[11,12]]->[C]
:"SAMPLE L VALUE"
:2->L
:"STORE REFERENCES TO THE"
:"MATRICES IN A STRING"
:"[A][B][C]"->Str1
:expr(sub(Str1, L, 1))->[J]
:"SHOWS 6"
:[J](1,2)
so then proceed normally with [J]
:If [J](E,F)
: "DO WHATEVER
Tested on an 84 SE, I assume it would work the same for anything in that family, except IIRC some older models only have matrices A-F
I am trying to better understand this syntax in Lua when using Torch:
local ten = torch.Tensor{{1,2,3},{4,5,6},{7,8,9}}
for i=1,(#ten)[2] do
print(ten[i][{{2}}])
end
Specifically the line
print(ten[i][{{2}}])
When I run the code, it prints
2
[torch.DoubleTensor of size 1]
5
[torch.DoubleTensor of size 1]
8
[torch.DoubleTensor of size 1]
I am familiar with table literals and their basic syntax, but what is the purpose of wrapping the '2' in double curly-braces, and how does it work in the engine?
The following answer was posted on GitHub when I asked the same question:
https://github.com/torch/torch7/issues/501#issuecomment-171290546
Have a look at this part of the documentation.
When you have single curly-braces, you are creating a selection of the tensor. So ten[{1}] is equivalent to ten[1], which is in turn equivalent to ten:select(1,1). If you have several indices like ten[{1,2}], this is also equivalent to the slower ten[1][2] (because the latter returns 2 times a tensor, whereas the former only returns it once). If you select on a 1D tensor, your output is a number.
When you have double curly-braces, it returns a narrow of the tensor, and a narrowed tensor is always a tensor (even if it only has one element). With double curly-braces, you can specify a range in which the tensor will be narrowed, which is not possible with single curly-braces. For example, you can do ten[{{1,2},1}], which will be a 1D tensor of dimension 2, and if your do ten[{{1,2},{2}}] it will return a 2D tensor of size 2x1.
For more information, have a look at select and narrow.
One last note, the doc for select is not precisely correct, as it's possible to do a select on a 1D tensor, and the output is a number.
I have 2 vectors of size 1x90.
I have to do the operator
diff=sum((V_new-V).^2);
But every time i do it i get the error:
Subscript indices must either be real positive integers or logical.
How can i fix this problem and prevent it from occurring again?
make sure you did not step over the sum function:
type
>> dbstop if error
run the code, it should stop in debugger when error occurred.
check what sum is:
>> which sum
should return that sum is a build-in function, but if you accidentally created a variable with that name, it will tell you that sum is a variable.
DO NOT USE BUILT-IN FUNCTIONS' NAMES AS VARIABLES
just wondering what the subtle difference between an array and a range is. I came across an example where I have x = *(1..10) output x as an array and *(1..10) == (1..10).to_a throws an error. This means to me there is a subtle difference between the two and I'm just curious what it is.
Firstly, when you're not in the middle of an assignment or parameter-passing, *(1..10) is a syntax error because the splat operator doesn't parse that way. That's not really related to arrays or ranges per se, but I thought I'd clear up why that's an error.
Secondly, arrays and ranges are really apples and oranges. An array is an object that's a collection of arbitrary elements. A range is an object that has a "start" and an "end", and knows how to move from the start to the end without having to enumerate all the elements in between.
Finally, when you convert a range to an array with to_a, you're not really "converting" it so much as you're saying, "start at the beginning of this range and keep giving me elements until you reach the end". In the case of "(1..10)", the range is giving you 1, then 2, then 3, and so on, until you get to 10.
One difference is that ranges do not separately store every element in itself, unlike an array.
r = (1..1000000) # very fast
r.to_a # sloooooow
You lose the ability to index to an arbitrary point, however.