Cartesian Product Of Sets — A Real Life Use Case In Python

If you’re like me then chances are that you set out to seek how anything you learn can be applied in real life. And, one of the things that fascinates me most is to see the application of abstract mathematical concepts, theories, and formulas to solve problems for humans.

Unfortunately, we don’t receive such education from our institutions and this has always left some emptiness and/or knowledge gap in curious souls like mine. I never appreciated mathematical discoveries like Linear Algebra (Matrices), Sets, Calculus, Probability & Statistics until I got introduced to Machine Learning. So, once I realized that such mathematical concepts were the life-blood of Machine Learning I got hooked onto ML and gave myself to intensive studies on these topics. I enjoy seeing and using mathematics like a tool rather than a mere medium for intellectual stimulation as we are trained in schools.

This same passion to know and apply mathematics led me to begin to think about how Cartesian Product of sets could be applied in real life. So, in this post I will show you how to apply this little mathematical concept in real-life and also show you how to automate it with simple one-liner python codes through list/set comprehension.

Introducing The Cartesian Product / Cross Product Of A Set

The cartesian product (or cross product) of A and B, denoted by A x B, is the set A x B = {(a,b) | a ∈ A and b ∈ B}. The elements (a,b) are ordered pairs. For example if A = {1,2} and B = {4,5,6} then the cartesian products of A and B is AxB = {(1,4),(1,5),(1,6),(2,4),(2,5),(2,6)}. Got it? Maybe one more example will help so lets consider the following sets:

A = {a,b} and B = {2,9} the the cartesian (or cross product) of A and B denoted by AxB = {(a,2),(a,9),(b,2),(b,9)}. This should be clear now, right? Cool! Formal education leaves us here wondering how on earth this is gonna be useful to us in anyway. How is this going to put food on our tables, provide shelter for our families, and pay the bills? After all, these are the reasons why we are advised (a very terrible advice for that matter) to study hard, pass our exams, hopefully get some good paying jobs, and hopefully have enough to care for our families. This terrible advice from parents and society is the the reason for the many frustrated unemployed graduates we see hanging around but this calls for a separate post.

How Will Cartesian Product Put Food On The Table?

As students we ask(ed) this question over and over but had no answers. We are not to blame anyway since the only reason we are advised to study hard is just so in the near future we can provide for our families. So, if the concept can’t bring food why should we even care?

Well, cartesian product can put food on your table so lend me your ear and understanding as I consider the following scenario with you.

Consider that you sell cars and a customer comes to you with the following request. “I need a number of cars. The models I want are Toyota Corolla, Honda CRV, and Chevy Cruze. And, for me to pay (putting food on the table)for your service I need my cars to be white, grey, and red and I want them to have 1.4, 1.6, and 1.8 engine capacities. I want a combination of cars that satisfy the specifications I’ve stated.”

After hearing this from the customer you pulled out your pen and started your calculations but after a while you are unsure if you understood what your customer asked for. Five minutes into your calculations (though this isn’t rocket science) your palms begin to sweat as you try to wrap your head around this request. Close to thirty minutes have passed since you had this request but you finally finished solving the customers exam question. You finally figured out that the customer actually want 27 cars with the following specs:

Make & Model : Toyota Corolla, Honda CRV, Chevy Cruze

Colors : White, Grey, Red

Engine Capacities : 1.4L, 1.6L, 1.8L

You figured this out someway but to be able to come up with such combination is the work of the Cartesian Product you learnt in school.

M = {Toyota Corolla, Honda CRV, Chevy Cruze}

C = {White, Grey, Red}

E = {1.4L, 1.6L, 1.8L}

Now the customer needs M x C x E period. Simple, ain’t it?

Automating Cartesian Product With Python

Now that we’ve developed the sets we need in our combination, let’s use the highly expressive python language to answer our question. But, before we do that lemme give you a refresher (or an introduction) to List Comprehension in python.

Consider that we have a list of numbers from 1 to 20 and we want to write code to select the multiples of 3 in this list. If you don’t know of list comprehension you’ll achieve this with a for-loop as shown below

for-loop solution

It took 5 lines of code to achieve our solution without list comprehension syntax in python. You remember I said “one-liner” at the beginning of this post right? Sure, there’s a one-liner solution and I will show you below

list comprehension solution

Wait, wait, wait, I know you want to shout “Holy cow!” if you’re new to list comprehension but don’t do that yet. Don’t play when you’ve a customer’s request to fulfill. You want to put food on the table, right?

set comprehension solution to customer’s request

You see, that easy. The variable products now holds information on all the 27 cars that our customer wants to pay us for. Note that because I used sets in my solution, the information in products is not guaranteed to be in order as sets an unordered. To keep some order, I’ll use lists as follows by changing the curly braces to square brackets.

list comprehension solution to customer’s request

Now, let’s see the details of the combination of 27 cars that the customers wants us to deliver. I know you can’t wait to receive the check but hey, no service, no pay!

output of solution

You got served, ain’t it?