Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of integer partitions when vector has negative values #18

Open
jwood000 opened this issue Jan 2, 2021 · 0 comments

Comments

@jwood000
Copy link
Owner

jwood000 commented Jan 2, 2021

There are many issues in the logic that assumes positive values. For example, there are many places where we check if the first element is zero. The vector is first sorted, thus when we have negative values, this check not valid. This causes strange behavior as we see below.

comboGeneral(-5:0, repetition = TRUE, constraintFun = "sum",
                                      comparisonFun = "==",
                                      limitConstraints = -5)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   -5    0    0    0    0    0
[2,]   -4   -1    0    0    0    0
[3,]   -3   -2    0    0    0    0
[4,]   -3   -1   -1    0    0    0
[5,]   -2   -2   -1    0    0    0
[6,]   -2   -1   -1   -1    0    0
[7,]   -1   -1   -1   -1   -1    0

The above example should look only have 5 columns. Observe:

comboGeneral(5:0, repetition = TRUE, constraintFun = "sum",
                                     comparisonFun = "==",
                                     limitConstraints = 5)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    5
[2,]    0    0    0    1    4
[3,]    0    0    0    2    3
[4,]    0    0    1    1    3
[5,]    0    0    1    2    2
[6,]    0    1    1    1    2
[7,]    1    1    1    1    1

Lastly, there is a significant performance penalty by all of those incorrect assumptions. The general algorithm (which is still efficient) is utilized in these situations when a much more efficient algorithm could be used.

## Optimized algorithm
system.time(comboGeneral(0:60, repetition = TRUE, constraintFun = "sum",
                                                  comparisonFun = "==",
                                                  limitConstraints = 60))
   user  system elapsed 
  0.086   0.061   0.147

## General algorithm
system.time(comboGeneral(0:-60, repetition = TRUE, constraintFun = "sum",
                                                   comparisonFun = "==",
                                                   limitConstraints = -60))
   user  system elapsed 
  0.718   0.348   1.066
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant