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

Help with puzzles 4 and 5 #9

Open
SnehaMondal opened this issue Jul 14, 2022 · 6 comments
Open

Help with puzzles 4 and 5 #9

SnehaMondal opened this issue Jul 14, 2022 · 6 comments

Comments

@SnehaMondal
Copy link

SnehaMondal commented Jul 14, 2022

I'm trying to solve puzzles 4 and 5 by using the predefined where(q, a, b) function, which expects q to be a boolean tensor. To arrive at a boolean tensor, I create a boolean list and use tensor on it. I suppose this is not allowed? How else could I implement this?

This is my current implementation for puzzle 5 (identity matrix of dimension j)

def eye(j: int) -> TT["j", "j"]:
   return where(tensor([[x==y for x in range(j)] for y in range(j)]), ones(j) - ones(j)[:, None] + ones(j), ones(j) - ones(j)[:, None])
@srush
Copy link
Owner

srush commented Jul 14, 2022

Everything you are currently doing with list comprehensions, you can do faster and simpler with the arange function .

For example if one does arange(10) == 2 that gives you a tensor that is all False with a True at position 2.

@atgctg
Copy link

atgctg commented Aug 21, 2022

After some experimentation, I came up with the following:

def eye(j: int) -> TT["j", "j"]:
   return where(arange(j) - arange(j)[:, None] == 0, 1, 0)

Wonder if there's even a better way...

@srush
Copy link
Owner

srush commented Aug 22, 2022

That's great! There are a couple ways to do it, but this is the way I did it.

The other thing I saw was to multiply to force type changes something like:

(arange(n)==arange(n)[:, None]) * 1

@CreativeSelf0
Copy link

CreativeSelf0 commented Mar 20, 2023

I really loved, the way you thought about it, it's out of the box thinking :P.
This is how I solved it. It would be cool if you have your solutions, out there. Although, it would incentives people to maybe give up easily. But person who gave up after trying would still learn.
a = tensor([0] * j) index = arange(j) data = outer(a,a) data[index, index] = 1 return data

@jselvam11
Copy link

where(arange(j)[:, None] == arange(j), (outer(ones(j), ones(j))), 0)

@CharlesDDDD
Copy link

CharlesDDDD commented Jun 21, 2024

return ((arange(j) + arange(j)[:,None]) == diag((arange(j) + arange(j)[:,None])))*1

Only after looking at your guys solutions did I realize how inelegant I am 🥹

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

No branches or pull requests

6 participants