CChainArbor
Workflows

Coding workflows · Matrix-chain cost planning

03
Built around your work

A concrete starting point

Find the minimum scalar-multiplication cost for a chain of up to 12 matrices. Test one matrix and different parenthesizations, rejecting dimensions outside 1 through 1000 without multiplying any matrix entries.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Matrix-chain cost planning

A concrete starting point
def chain_cost(dimensions):
    if type(dimensions) not in (list, tuple) or not 2 <= len(dimensions) <= 13:
        raise ValueError("one through twelve matrices")
    if any(type(d) is not int or not 1 <= d <= 1000 for d in dimensions):
        raise ValueError("dimensions 1 through 1000")
    n = len(dimensions)-1
    cost = [[0]*n for _ in range(n)]
    for width in range(2, n+1):
        for left in range(n-width+1):
            right = left+width-1
            cost[left][right] = min(
                cost[left][k]+cost[k+1][right]+
                dimensions[left]*dimensions[k+1]*dimensions[right+1]
                for k in range(left,right))
    return cost[0][n-1]

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗