How to add new element to array?

How to add new element to array?

g2m.agent
Collaborator Collaborator
1,742 Views
7 Replies
Message 1 of 8

How to add new element to array?

g2m.agent
Collaborator
Collaborator

Suppose I want to divide a value equally, and then add these values together and store them in an array. For example, 90 is divided into 3 equal parts, and the cumulative result is [30, 60, 90]. I can do this in Python:

v = 90 # value
max = 3 # max_iterations
i = 1 # current_index
step = i/max
degrees = [] # output array
while i <= max:
    degree = step*v*i
    degrees.append(degree)
    i += 1
print(degrees)
# Result: [30.0, 60.0, 90.0] #

 But how to do this in bifrost?

My graph is not working...

outside:

maya_wpxUhW0Rrm.jpg

inside

maya_k3eGd6owBq.jpg

I got warning:

"output", Warnings: - array Warning: while compiling top level compound "bifrostGraphShape1", around the port 'bifrostGraphShape1::iterate.array': no promotion exists from a value of type 'array<float>' to a value of type 'float'

 

why?

0 Likes
Accepted solutions (2)
1,743 Views
7 Replies
Replies (7)
Message 2 of 8

mjcg91
Collaborator
Collaborator
Accepted solution

Ports states has to be the same data type. In this case you want to append the computed value on an empty array on which you append a value at each iterations.

build_array_inside_loop1.jpgbuild_array_inside_loop2.jpg

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
Message 3 of 8

g2m.agent
Collaborator
Collaborator
It works. But I don't understand how this iteration works. why doesn't "build_array" become a 2d array? this "visualization" is more confusing than the code.
0 Likes
Message 4 of 8

mjcg91
Collaborator
Collaborator

build_array works like append in Python. Is will either create an array from a single value or append values to an existing array.

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
Message 5 of 8

mjcg91
Collaborator
Collaborator

Btw if all you want to do is like the function above, you might as well do it in a for-each loop and directly plug the value to the output. It's gonna be much faster. If your array's construction is not based on a conditions, or if you don't have to access the active array data during the loop, there is no reason tobuse iterate or do-while loops. 

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
0 Likes
Message 6 of 8

g2m.agent
Collaborator
Collaborator

the for-each loop gives me 2d array:

maya_UYVxDd8Ol1.jpg

inside:

maya_BVLlSKGjko.jpg

0 Likes
Message 7 of 8

mjcg91
Collaborator
Collaborator
Accepted solution

you don't need a build array node inside for each, nor have to init an empty array. Just plug the single values to the output. Also pay attention to port icons.

Maxime Jeanmougin - Technical Artist
https://maximejeanmougin.com

Join the Bifrost Addicts community on Discord:
https://discord.gg/bifrost-addicts
Message 8 of 8

g2m.agent
Collaborator
Collaborator

thank you! that helps a lot.