On Sunday, February 10, 2019 at 4:47:52 PM UTC+8, Philip McGrath wrote:
>
> You may also want to look into the math/array 
> <https://docs.racket-lang.org/math/array.html> and math/matrix 
> <https://docs.racket-lang.org/math/matrices.html> libraries.
>


looking at the code, it seems to me that each iteration only the "current" 
and "next" generation are needed.  The best structure might be a list of 
vectors which is constructed step by step -- every iteration a new vector 
is added to this list.   This assumes that the entire "evolution" is needed 
at the end.  If all is needed at the end is the "current" generation, it 
might be more efficient to just use two vectors, the current and next one 
and swap them each iteration.

But before we get into improving the code, let's get it working first :-)

Alex.

 

> -Philip
>
>
> On Sun, Feb 10, 2019 at 3:42 AM Alex Harsanyi <alexha...@gmail.com 
> <javascript:>> wrote:
>
>> This line looks suspicious:
>>
>>      (define results (make-vector years (make-vector (vector-length 
>> fecundity) 0)))
>>
>> The "(make-vector (vector-length fecundity) 0)" expression will create a 
>> single vector, than it creates the outer vector will all elements pointing 
>> to it.  It is not a matrix, but a "column" vector where each element is 
>> referencing the same row vector.  This means that if you update an element 
>> in one of the rows, the same value will "appear" in all other rows. The 
>> only row that is different is the first one which you initialize in the 
>> line:
>>
>>     (vector-set! results 0 (make-vector (vector-length fecundity) 10))
>>
>> What you probably want is a vector of vectors, which can be built like 
>> this
>>
>>     (define results (for/vector ([index (in-range years)]) (make-vector 
>> (vector-length fecundity) 0)))
>>
>> Alex.
>>
>>
>>
>> On Sunday, February 10, 2019 at 3:40:42 PM UTC+8, travis.h...@gmail.com 
>> wrote:
>>>
>>> Hi All,
>>>
>>> I'm an R programmer that has recently started learning Racket. I decided 
>>> to start by trying to create a simple age-structured population model. In 
>>> R, I would initialize a matrix and use nested for loops to move through the 
>>> elements of the matrix and propagate the population forward through time. 
>>> For my first attempt in Racket (
>>> https://gist.github.com/hinkelman/3ee6115cdd7f0a4c8f1672b7d8df5c27), I 
>>> used for* to loop through a vector of vectors. The code in that gist 
>>> doesn't quite work. There is apparently something wrong with how I'm using 
>>> vector-set! such that "rows" in my vector of vectors are being updated 
>>> prematurely. I would greatly appreciate it if someone could point out what 
>>> I'm doing wrong there. I'm also interested in suggestions for alternative 
>>> approaches because it seems unlikely that I have written this code in 
>>> idiomatic Racket.
>>>
>>> Thanks,
>>>
>>> Travis
>>>
>>> P.S. If it is helpful, here is a gist (
>>> https://gist.github.com/hinkelman/d5b8414b0c6383057d7846509a724bbf) 
>>> with the R code that I was trying to write in Racket.
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to