A furniture workshop supplies several showrooms and plans next month's materials with two tables:
orders[s][p]: pieces of product p ordered by showroom s (one row per showroom, one column per product);materials[p][g]: amount of material g (metres of timber, litres of varnish, ...) that one piece of product p uses (one row per product, one column per material).The matrix product orders materials is a showrooms materials table: how much of each material every showroom's whole order needs.
The buyers want something finer: the materials needed for only some of the products, for example how much varnish goes into the chairs and the shelves alone, showroom by showroom.
A useful tool here is the outer product. For a column with entries and a row with entries, the outer product is the table whose entry in row , column is : every entry of times every entry of .
Task: write catalogue_demand(orders, materials, products) returning the showrooms materials table needed to make just the products whose indices are listed in products (no repeats, any order), as a list of rows with every entry rounded to 4 decimal places.
products is empty, nothing is made: return a table of zeros of the right shape.products lists every product, the answer is the full product orders materials.