cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Mathcad programming

NM_10165576
11-Garnet

Mathcad programming

Hello everyone,
i want to program this in Mathcad. Could someone help me with this?

 

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:NM_10165576)

Is this of any help?

 

2024 10 16 D.png

 

Stuart

View solution in original post

4 REPLIES 4
StuartBruff
23-Emerald III
(To:NM_10165576)

Is this of any help?

 

2024 10 16 D.png

 

Stuart

Thank you.

Could you please give me brief explanation

StuartBruff
23-Emerald III
(To:NM_10165576)


@NM_10165576 wrote:

Could you please give me brief explanation


 

Sure. The function generate takes a single argument, n.

 

The matrix L stores each permutation of n, one per row.

 

The outer k-loop runs over each value in the first row of n; The 1st and k-th values of n exchange places at the start of the loop.  This has the effect of leaving the rest of n in the correct lexicographical order. (the first swap when k = 0 leaves n unchanged but it's not worth the brain clock cycles trying to do anything fancy to make it more efficient)

 

The inner i-loop iterates over each permutation of n in its k-th state.  m is the permutation counter and its binary representation as a vector allow generate to select which of the n elements to copy to matrix L and which to set to zero.  (Note: If a Mathcad matrix (or any array) element isn't explicitly set to some value, it defaults to zero.  If I'd used an "if" programming statement instead of an "if" function, I could have used the behaviours, but the if function allows you to set a default value other than zero should you so wish).

 

Rinse and repeat until generate has run its course.

 

I'm sure there are multiple better ways to solve the problem, but that's the method that first occurred to me.

 

Stuart

 

Announcements

Top Tags