Questions tagged [cartesian-product]
The Cartesian product, named after René Descartes, of two sets A and B is the set of all ordered pairs ab, where a is element of A and b is element of B.
833
questions
-1
votes
0
answers
11
views
How does sigma (σ) (select) (restrict) work when applied to a naturally joined table?
How does sigma (σ) (select) (restrict) work when applied to a naturally joined table?
I have two tables A(idA, nameA) and B(idB, idA, nameB) with idA from B being a foreign key referencing idA in A.
...
2
votes
3
answers
97
views
Flatten nested JSON to array of unique objects without indexes
I have this simple nested object which I need to flatten to be able to insert it into my database.
const input = {
name: "Benny",
department: {
section: "Technical",
...
-2
votes
0
answers
21
views
Combination of product and sum rule [closed]
How many passwords of length 5 to 7 are possible if it is known that a password must contain one digit, only lowercase letters and digits are allowed?
Explain the process how can I get the answer.
1
vote
1
answer
49
views
Some combinations of nested dictionaries with arbitrary lengths in python
I have a game tree with an arbitrary number of possible moves for me and my opponent with an arbitrary sequence of moves. I want to get an overview of all 'sets of moves I can make' and corresponding ...
1
vote
2
answers
51
views
Shuffle a cartesian product iterator in Javascript
Using a library like https://www.npmjs.com/package/cartesian-product-generator I am able to generate a cartesian product iterator. This is necessary when I have, say 4 arrays, each of length 100, ...
0
votes
1
answer
58
views
DAX: Unwanted cartesian product lines?
Please help to calculate/understand properly lastDate and rankDate measures for following simplified example (download):
Desired result:
Reality (incorrect subtypes):
Why relationship is broken?
...
1
vote
2
answers
72
views
Given the list, how to generate a nested dictionary
Given the list below, using Python 3 how can I generate a nested dictionary with list[0] having list[1:] as nested children.
list = [
["Layer 1.A", "Layer 1.B"],
["...
3
votes
1
answer
35
views
How to stop pandas merge() function to do cartesian product when key values are repeated?
a = pd.DataFrame({"a": [1,2], "b": [1,1]})
b = pd.DataFrame({"c": [3,4], "b": [1,1]})
c = a.merge(b, on=['b'], how='inner')
we will get c having 4 lines. But ...
2
votes
1
answer
80
views
"Distributive property" with Shapeless
Not sure if the correct term is "distributive property" but I remember learning this in school so here's an example of what I'm trying to do:
Given:
type MyHList = (A :+: B :+: C :+: CNil) ::...
0
votes
1
answer
38
views
multiply 3 strings arrays python
a= ['M', 'K', 'J']
b= ['B', 'C', 'D', 'T','A']
d= ['A','B','C','Z']
I want to multiply 3 arrays
first output would be:
MBA
MCA
MDA
MTA
MAA
0
votes
1
answer
32
views
Fill up DataFrame counting to full cartesian product
Look at this code:
result=pd.DataFrame(df.groupby(['col1','col2'])['col3'].count())
It basically does what I want to with one minor issue: I want the result to have the full cartesian product of all ...
0
votes
1
answer
30
views
How can I match a row in SQL Server only once?
I have the following problem where I am kindly asking for your help when joining two tables in SQL Server 2016 (v13).
I have 2 tables, Revenues and Cashins.
Revenues:
RevenueID
ProductID
InvoiceNo
...
0
votes
2
answers
43
views
Match iteration number to values of non existent Cartesian Product Array
Lets say we have the following arrays.
[1,2] & [1,2,3] & [1,2,3,4]
Then let's say we want to loop through all unique possible combinations of this.
The results should look something like this.
...
0
votes
1
answer
45
views
Find out all possible cartesian products using Recursion
The function takes in a string (str) s and an integer (int) n .
The function returns a list (list) of all Cartesian product of s with length n
The expression product(s,n) can be computed by adding ...
0
votes
1
answer
38
views
Use Python's Numba library to calculate and iterate over the itertools.product of an array
How would I go about doing this? I would like to be able to do this, but the itertools library doesn't work with Numba's njit.
I have tried many answers from online, but they all throw errors I cannot ...
0
votes
0
answers
29
views
Is the following sequence performant: dataframes cartesian product -> groupby -> reduce
I'm interested in performing a groupby-reduce operation on the cartesian product between two dataframes (say df1 and df2).
With my current implementation, these dataframes are fairly big (GBs) and I ...
0
votes
4
answers
83
views
Combine the array of strings in Bash
I have:
TEXT='1,2
a,b,c
XX,YY'
I need the output like this:
OUT='1,a,XX
1,a,YY
1,b,XX
1,b,YY
1,c,XX
1,c,YY
2,a,XX
2,a,YY
2,b,XX
2,b,YY
2,c,XX
2,c,YY'
Should be some recursion, because the length of ...
0
votes
1
answer
112
views
How to make combination of a string using list of words for each position
I have a list of strings like this:
original_text = "womens wear apparel bike"
Now, each word of original_text will have alternative words, like this list:
text_to_generate = [['females', '...
-2
votes
2
answers
39
views
Combination between two lists using cartesian product with a small twist
I am trying to do an algorithm where I find all the possible combination between two lists but I am not sure how do we call this type of combination in math:
Input:
List 1 : [a,b,c,d] and List 2 : [1,...
1
vote
2
answers
53
views
How to Group Element in Javascript to get the Cartesian product in javascript
I have a scenario where I have an object just like this.
[
{attributeGroupId:2, attributeId: 11, name: 'Diamond'}
{attributeGroupId:1, attributeId: 9, name: '916'}
{attributeGroupId:1, ...
0
votes
1
answer
35
views
Product of Two Dict Values while retaining Dict Key
I have a dictionary of an unknown amount of keys, and I'm trying to get the products of all the keys combined, while still retaining which list the items came from. For example
dicts = {
"d1&...
1
vote
1
answer
109
views
C++20 range from enum
can an enum be used as (or turned into) a C++20 range?
I am thinking about following use case, cartesian_product() is C++23 unfortunatey:
#include <algorithm>
using namespace std::ranges;
enum ...
3
votes
1
answer
54
views
Fill the Third Array by adding the elements of the First and Second Arrays
I want to generate an array which content would represent a Cartesian product of the two given string-arrays.
In other words, I need to concatenate each String from the first array arr1 with every ...
0
votes
1
answer
77
views
How to get all combination between N objects with K values C#
I'm looking for an algorithm that can solve my issue.
I'm working on a project which deals with products and their attributes.
Here I have a product that has N number of options and the options each ...
0
votes
0
answers
75
views
Using pure numpy instead of multiple nested for loops that include a calculation with changing values
The following function uses nested for loops that include a calculation used to populate a numpy array:
def foo():
# Initializing variables
xrange = 2
yrange = 2
t_array = np.random.randint(1, ...
0
votes
2
answers
60
views
Apply function to cartesian product of numeric and function type
I have a function
eval_ = function(f, i) f(i)
for a list of functions, say
fns = list(function(x) x**2, function(y) -y)
and a vector of integers, say
is = 1:2
I would like to get eval_ evaluated at ...
0
votes
1
answer
46
views
How to create a eventually fair but biased distribution of Cartesian product of sets
Say I have X or 3 sets of different sizes.
So 3×4×5
I want to generate a probability distribution of the Cartesian of these sets that eventually and is guaranteed to cover every item.
Which based on ...
1
vote
0
answers
77
views
Calculate sum and cumul by age group and by date (but people changes age group as time passes)
DBMS : postgreSQL
my problem :
In my database I have a person table with id and birth date, an events table that links a person, an event (id_event) and a date, an age table used for grouping ages. In ...
0
votes
2
answers
105
views
What's the mathematical formula for a nested loop?
Imagine this loop:
for i in range(3):
for j in range(3):
for k in range(3):
print("{}{}{}".format(letter, number, symbol))
I want to generate this sequence numerically - without a ...
0
votes
1
answer
63
views
Combination of Keys with Empty Tuple
I have a dictionary with keys. The keys are tuple of two.
dict_keys([(), ('cHW',), ('cHWtil',), ('cHW', 'cHW'), ('cHWtil', 'cHWtil'),('cHW', 'cHWtil'))
However the tuple can also be a combination of ...
1
vote
3
answers
94
views
Get all combinations of changing characters in a sring (C++)
Okay, I've been tearing my hair out for the past 5 hours on this.
I'm still new to C++, so bear with me if this is a stupid question.
I would like to get all possible character combinations, and put ...
0
votes
2
answers
73
views
Hi I am new to Neo4j, I am getting error with cartesian product
I am getting error with cartesian product. like I am trying to create a relationship, But it does-not allow me to create relationship instead it says no changes. I am not able to create create ...
2
votes
2
answers
53
views
R: cross join column on itself excluding symmetric results
I have a data table init that looks like this:
> init
+---+
| id|
+---+
| a|
+---+
| b|
+---+
| c|
+---+
I want to obtain all pairs for id column, so I need to cross join the init data ...
1
vote
1
answer
55
views
How do I get the cartesian product of a dictionary in a specific order?
I want to create a DF of the possible combinations from a full factorial design of experiments.
I'm using doepy but it seems to be taking a slowing down as the number of combinations in my DOE grows. ...
0
votes
1
answer
32
views
Eliminate same configuration product
I have three places, and each place can take a value from the set {1, 2, 3}.
I am using the following python script to generate all the possible product:
import itertools
options = [1, 2, 3]
for ...
0
votes
1
answer
55
views
All combinations of arbitrary number of sets using for comprehension
Let's look at following source of code:
def foo(s1: Set[Int], s2: Set[Int], s3: Set[Int]): Set[Set[Int]] = {
for {
ss1 <- s1
ss2 <- s2
ss3 <- s3
} yield Set(ss1, ss2, ss3)
}
...
0
votes
1
answer
116
views
Get all cartesian products of a set of strings of different lengths, that add up to a total length of n
I'm trying to generate all multiple cartesian products of a set S with itself of a fixed total target string length n, with the set containing strings of different lengths.
Eg. S = ['1', '22', '333'] ...
-1
votes
1
answer
23
views
Cartesian product- inner relations in table. SQL
I have a table built of two columns: person (text) and supervisor (text)- a sort of hierarchy table.
table name: hierarchy
person
supervisor
Bob
Alice
Mary
Susan
Alice
David
David
Mary
I wish to ...
0
votes
0
answers
83
views
ways to solve optimization problem with cartesian product and restrictions in a production line
I'm new to this kind of problem and I'm having a lot of difficulty in understanding how to solve this problem: let's suppose we have 3 types of objects to produce in a production line (A, B, C), I ...
3
votes
1
answer
513
views
PySpark cross join excluding symmetric results
I have a Spark dataframe:
> df
+---+
| id|
+---+
| a|
+---+
| b|
+---+
| c|
+---+
I want to obtain all pairs for id column, so I need to cross join the dataframe on itself. But I want to ...
0
votes
2
answers
420
views
cartesian product on two tables
I'm trying to create a result set of table names and column names. In table A I have the list of table names and in Table B I have the list of column names. Each table should have the full list of ...
-1
votes
1
answer
216
views
Cartesian product but remove duplicates up to cyclic permutations
Given two integers n and r, I want to generate all possible combinations with the following rules:
There are n distinct numbers to choose from, 1, 2, ..., n;
Each combination should have r elements;
...
0
votes
0
answers
16
views
Plotting Cartesian Product of two infinite sets
The problem regarding plotting of Cartesian product of two finite sets have been dealt with previously in this forum.
My concern is that I have two infinite sets and I want to plot the cartesian ...
1
vote
2
answers
104
views
Compile-time efficient n-ary cartesian product of parameter packs with a transformation
In a previous question, solutions were given on how to compute the n-ary cartesian product of parameter packs (see here, here (and here but for tuples)). Basically, we consider the following wrapper:
...
0
votes
1
answer
35
views
Cartesian product in R
What is the fastest way to find cartesian product of two lists in R? For example, I have:
x <- c(a,b,c,d) y <- c(1, 2, 3)
I need to make from them the following data.frame:
x y
1 a 1
2 a 2
...
0
votes
0
answers
78
views
How to implement the division of two relations in mapreduce?
I want to implement the division of two relations in MapReduce. I have two relations: T(A,B,C) and U(B,C). I know that
for the relations R(A,B,D) and S(A,B). This is pretty much my scenario. I am not ...
2
votes
1
answer
39
views
Is there a pandas way to add a dataframe for each row by a specific column value
I have a dataframe here:
name role
0 Allen Director
1 Kendrick Food
2 Sean Webmaster
3 Jacob PR
I also have another dataframe:
power
0 eat
1 sleep
2 code
...
0
votes
2
answers
214
views
How to create Product Attribute Combination in asp.net core?
Product Name
Color
Size
T Shirt
Red
Small
Pant
Yellow
Large
Green
Desired results:
T Shirt Red Small
T Shirt Red Large
T Shirt Yellow Small
T Shirt Yellow Large
T Shirt Green Small
T Shirt ...
0
votes
1
answer
51
views
Google Apps Script Transpose & Cartesian Product
I have been building a solution for project resource management in Google Sheets.
The idea is that the team manager would input the resource demand per project on a weekly level on a single row. This ...
0
votes
0
answers
45
views
More stable/efficient cartesian product function to determine all combinations n in length
I am working on an error correction method for DNA data storage. It works by identifying errors and then rapidly substituting all possible combinations of bases into error positions until the data ...