Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasting? #108

Closed
xloem opened this issue Jul 3, 2020 · 6 comments
Closed

Broadcasting? #108

xloem opened this issue Jul 3, 2020 · 6 comments
Labels

Comments

@xloem
Copy link

xloem commented Jul 3, 2020

I'm unsure how to broadcast assignment to across columns or rows.

The wiki states:

// assign a vector to all the columns of a 3D array
A(0,0,all) = vec(all);

But this looks like it selects the first column and first row (index 0,0): what is the actual situation here?

@romeric
Copy link
Owner

romeric commented Jul 3, 2020

Are you comparing Fastor with MATLAB/Fortran/Julia style broadcasting?

Note that Fastor tensors are row-major (C-style or NumPy style) so the order is swapped in that:

Tensor<double,3> vec = {1,2,3};      // a vector
Tensor<double,2,4,3> A(0);             // 3D tensor of all zeros 
A(0,0,all) = vec(all);
print(A);

will print

[0,:,:]
[1, 2, 3]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[1,:,:]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

Here A is a 3D tensor made up of two 4x3 matrices.

So in A(i,j,k), i will give you the ith 4x3 matrix, j will give you the jth row in that matrix and k will give you the kth column in that matrix.

So in A(0,0,all) you are asking Fastor to give you the first 4x3 matrix and from that matrix give you the first row and all the columns.

Hope that is clear.

@xloem
Copy link
Author

xloem commented Jul 5, 2020

I see ... so by "assign a vector to all the columns" you mean that each column gets one different element of the vector.
How do we assign the same vector to every full column? such that all rows are filled with the same value, different for each row, and all columns become the same new vector?

@romeric
Copy link
Owner

romeric commented Jul 5, 2020

Right I see that the documentation is incorrect. I will correct that. We don't have much documentation for Fastor at the moment.

What you are asking is essentially tiling numpy.tile, numpy.repeat or MATLAB's repmat feature to broadcast a vector to all elements of a tensor. This feature is unfortunately not implemented yet, but there is a high request rate for it. I will implement this as soon as I get some time.

Something like

Tensor<double,1,3> vec = {{1,2,3}};
Tensor<double,3,3> A = repeat<1,3>(vec); // repeat 3 times, column-wise (1 for column-wise and 3 for number of repetition)
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]

For the time being however, unfortunately you will have to implement this with for loops

@xloem
Copy link
Author

xloem commented Jul 6, 2020

Thanks, that's very clarifying. Maybe I can try to stub an interface out with a naive implementation if I am bored, and submit a PR.

@xloem xloem closed this as completed Jul 6, 2020
@romeric
Copy link
Owner

romeric commented Jul 7, 2020

You are welcome to submit a PR if you wish

@romeric
Copy link
Owner

romeric commented Jul 7, 2020

Also see #103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants