Title
h

Hullaballoonatic

05/10/2019, 8:43 PM
is there a list building method that can reference previously constructed elements of the list while building it that i'm not seeing? Or does the collection not exist until after all its elements are determined, so that's not really possible in as a List constructor
l

Luke

05/10/2019, 8:55 PM
If you start with a collection or some sort of iterable, maybe you can use
``.fold``
. It uses an accumulator that is passed in the lambda
h

Hullaballoonatic

05/10/2019, 9:30 PM
yeah, i was thinking fold, but wouldn't that be crazy expensive?
oh, well i could fold into a mutable list
k

karelpeeters

05/10/2019, 9:34 PM
Or you know, sometimes a for loop is fine as well.
h

Hullaballoonatic

05/10/2019, 9:34 PM
yeah, i just figured i could make it all pretty
i think if i did it as a sequence then i could pull elements from early parts of the list while i built it
this feels clever but obnoxious...
``````val (u: List<Vector>, e: List<Vector>) = cols.foldIndexed(ArrayList<Vector>(width) to ArrayList<Vector>(width)) { k, (u, e), a ->
u[k] = when(k) {
0 -> a
1 -> a - a * e[0] * e[0]
else -> a - e.map { a * it * it }.sum()
}
e[k] = u[k] / u[k].magnitude
u to e
}``````
it's a translation of this:
i think i can simplify it all actually
k

karelpeeters

05/10/2019, 9:55 PM
I think a for loop with a sum in it is way clearer.
h

Hullaballoonatic

05/10/2019, 9:55 PM
yep
you're right
k

karelpeeters

05/10/2019, 9:57 PM
``````val U = mutableListOf<Vector>()
for (a in A)
U += (a - U.sumBy { it * a * a }).unit``````
h

Hullaballoonatic

05/10/2019, 9:58 PM
i'll make it even better actually. i'll give you the result
when i'm done
k

karelpeeters

05/10/2019, 9:59 PM
Now I'm curious ðŸ˜’imple_smile:
h

Hullaballoonatic

05/10/2019, 10:39 PM
``````val e = arrayListOf<Vector>()
cols.forEachIndexed { k, a ->
e[k] = when (k) {
0 -> a
1 -> a - a * e[0] * e[0]
else -> a - e.map { a * it * it }.sum()
}
}``````
k

karelpeeters

05/10/2019, 10:40 PM
You don't need special cases for 0 and 1 though, right?
h

Hullaballoonatic

05/10/2019, 10:41 PM
i don't know why it pastes all skewed like that
k

karelpeeters

05/10/2019, 10:41 PM
Because you didn't select the indent of the first line.
h

Hullaballoonatic

05/10/2019, 10:41 PM
but yeah, you're right that i don't have to, although it might speed it up a tad, and also it helps for clarity
ah, you're absolutely correct
anyways, u only exists to define e
in the algorithm, so i cut out the middleman
k

karelpeeters

05/10/2019, 10:42 PM
It's the other way around no?
h

Hullaballoonatic

05/10/2019, 10:43 PM
that's what it seems to be reading top to bottom
but this is qr decomposition, and neither q nor r are defined by u
only e
k

karelpeeters

05/10/2019, 10:44 PM
Ah I thought you just wanted to do Gramâ€“Schmidt.
h

Hullaballoonatic

05/10/2019, 10:44 PM
this is the gram-schmidt qr decomp algorithm
i just posted the excerpt concerning the block of code i wrote for context
k

karelpeeters

05/10/2019, 10:44 PM
I see.