Hi, I'm new to sagemath, and I've been away from math courses for a while now. Please be patient with me! :)
I'm trying to use sagemath to identify all integer solutions to a set of linear equations. However, the "linear equations" can be a mixture of equalities and inequalities depending on the situation. I'll use a classic math textbook problem to illustrate.
Assume you have a collection of pennies, nickels, and dimes. Further, you want to identify all combinations of coins that sum to an exact value of >= 10 cents AND <= 15 cents. Here are two twists: you will only select from 5 to 7 coins, and only nickels and dimes will contribute to the wanted value. Assume you have more than 7 of each type of coin.
In the equations below, 'pennies', 'nickels', and 'dimes' represent the number of coins from the respective denomination. So, the equations are:
pennies + nickels + dimes >= 5
pennies + nickels + dimes <= 7
5*nickels + 10*dimes >= 10
5*nickels + 10*dimes <= 15
There are many solutions to this system (set notation used for pennies to conserve space):
pennies = [4,6], nickels = 0, dimes = 1
pennies = [3,5], nickels = 1, dimes = 1
pennies = [3,5], nickels = 2, dimes = 0
pennies = [2,4], nickels = 3, dimes = 0
I want to know if sagemath can produce a list of dictionary solutions for all positive integer solutions. For example:
[
{ pennies: 4, nickels: 0, dimes: 1 },
{ pennies: 5, nickels: 0, dimes: 1 },
{ pennies: 6, nickels: 0, dimes: 1 },
{ pennies: 3, nickels: 1, dimes: 1 },
...
{ pennies: 4, nickels: 3, dimes: 0 }
]
This is what I've tried in sagemath so far:
sage: pennies,nickels,dimes = var('pennies,nickels,dimes')
sage: coins_max = (pennies+nickels+dimes>=5)
sage: coins_min = (pennies+nickels+dimes<=7)
sage: value_max = (5*nickels+10*dimes<=15)
sage: value_min = (5*nickels+10*dimes>=10)
sage: assume( pennies>=0, pennies<=7, nickels>=0, nickels<=7, dimes>=0, dimes<=7 )
sage: coin_solve = solve( [coins_max, coins_min, value_max, value_min], pennies, nickels, dimes, solution_dict=True )
sage: print coin_solve
[
{-2*pennies + 8: nickels, dimes: -1/2*nickels + 1, nickels: -2*pennies + 12},
{-2*pennies + 7: nickels, dimes: -1/2*nickels + 3/2, nickels: -2*pennies + 11},
{-2*pennies + 7: nickels, dimes: -nickels - pennies + 5, nickels: -2*pennies + 8},
{-2*pennies + 11: nickels, dimes: -nickels - pennies + 7, nickels: -2*pennies + 12},
{-2*pennies + 7: nickels, max(-1/2*nickels + 1, -nickels - pennies + 5): dimes, dimes: min(-1/2*nickels + 3/2, -nickels - pennies + 7), nickels: -2*pennies + 12},
{pennies: dimes + 3, nickels: -2*dimes + 2},
{pennies: dimes + 5, nickels: -2*dimes + 2},
{pennies: dimes + 2, nickels: -2*dimes + 3},
{pennies: dimes + 4, nickels: -2*dimes + 3}
]
This is where my unfamiliarity with sagemath becomes a problem. I don't know if solve() is the right approach, if there's another function I should call to process the results into a list of integer solutions, or if I have to write my own.
I'm hoping someone can point me in the right direction. Thank you for your time.