cross-product

How to select specific item from cartesian product without calculating every other item

落花浮王杯 提交于 2019-11-27 21:20:55
I'm mostly convinced that there is an answer to this problem, but for the life of me can't figure out how to do it. Let's say I have three sets: A = [ 'foo', 'bar', 'baz', 'bah' ] B = [ 'wibble', 'wobble', 'weeble' ] C = [ 'nip', 'nop' ] And I know how to calculate the cartesian / cross product, (ant it's covered all over the place, on this site and elsewhere) so I won't go over that here. What I'm looking for is an algorithm that would allow me to simply select a specific item from the cartesian product without generating the whole set or iterating until I reach the nth item. Of course, I

Cross product in Scala

核能气质少年 提交于 2019-11-27 13:25:39
I want to have a binary operator cross (cross-product/cartesian product) that operates with traversables in Scala: val x = Seq(1, 2) val y = List('hello', 'world', 'bye') val z = x cross y # i can chain as many traversables e.g. x cross y cross w etc assert z == ((1, 'hello'), (1, 'world'), (1, 'bye'), (2, 'hello'), (2, 'world'), (2, 'bye')) What is the best way to do this in Scala only (i.e. not using something like scalaz)? You can do this pretty straightforwardly with an implicit class and a for -comprehension in Scala 2.10: implicit class Crossable[X](xs: Traversable[X]) { def cross[Y](ys:

How to select specific item from cartesian product without calculating every other item

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 04:30:16
问题 I'm mostly convinced that there is an answer to this problem, but for the life of me can't figure out how to do it. Let's say I have three sets: A = [ 'foo', 'bar', 'baz', 'bah' ] B = [ 'wibble', 'wobble', 'weeble' ] C = [ 'nip', 'nop' ] And I know how to calculate the cartesian / cross product, (ant it's covered all over the place, on this site and elsewhere) so I won't go over that here. What I'm looking for is an algorithm that would allow me to simply select a specific item from the

Cross product in Scala

空扰寡人 提交于 2019-11-26 16:23:45
问题 I want to have a binary operator cross (cross-product/cartesian product) that operates with traversables in Scala: val x = Seq(1, 2) val y = List('hello', 'world', 'bye') val z = x cross y # i can chain as many traversables e.g. x cross y cross w etc assert z == ((1, 'hello'), (1, 'world'), (1, 'bye'), (2, 'hello'), (2, 'world'), (2, 'bye')) What is the best way to do this in Scala only (i.e. not using something like scalaz)? 回答1: You can do this pretty straightforwardly with an implicit

Computing the cross product of two vectors in Fortran 90

烂漫一生 提交于 2019-11-26 10:35:51
I would like to compute the cross product of two vectors in Fortran 90. For example, in words, the cross product of (1, 2, 3) and (4, 5, 6) turns out to be (-3, 6, -3) in Cartesian coordinates. I wrote the following code (main program followed by function definition): PROGRAM crosstest IMPLICIT NONE INTEGER, DIMENSION(3) :: m, n INTEGER, DIMENSION(3) :: cross INTEGER, DIMENSION(3) :: r m=(/1, 2, 3/) n=(/4, 5, 6/) r=cross(m,n) END PROGRAM crosstest FUNCTION cross(a, b) INTEGER, DIMENSION(3) :: cross INTEGER, DIMENSION(3), INTENT(IN) :: a, b cross(1) = a(2) * b(3) - a(3) * b(2) cross(2) = a(3) *

Computing the cross product of two vectors in Fortran 90

时光总嘲笑我的痴心妄想 提交于 2019-11-26 01:54:22
问题 I would like to compute the cross product of two vectors in Fortran 90. For example, in words, the cross product of (1, 2, 3) and (4, 5, 6) turns out to be (-3, 6, -3) in Cartesian coordinates. I wrote the following code (main program followed by function definition): PROGRAM crosstest IMPLICIT NONE INTEGER, DIMENSION(3) :: m, n INTEGER, DIMENSION(3) :: cross INTEGER, DIMENSION(3) :: r m=(/1, 2, 3/) n=(/4, 5, 6/) r=cross(m,n) END PROGRAM crosstest FUNCTION cross(a, b) INTEGER, DIMENSION(3) ::