-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploying to gh-pages from @ c1a6b0f 🚀
- Loading branch information
1 parent
904e0d9
commit 2cf9a50
Showing
8 changed files
with
254 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<div class="sect1"> | ||
<h2 id="_description">Description</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>This rule raises an issue when a NumPy function expecting a square matrix is called with a non-square matrix.</p> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>Some matrix operations only make sense on square matrices. For example, a determinant can be computed only if a matrix is square.</p> | ||
</div> | ||
<div class="paragraph"> | ||
<p>Therefore, NumPy functions performing these operations should be provided with a square matrix as an argument. If something other than a square matrix is provided, the computation does not make sense and an exception will be raised.</p> | ||
</div> | ||
<div class="paragraph"> | ||
<p>The following functions will raise an exception when provided with a non-square matrix:</p> | ||
</div> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p><code>np.linalg.det(a)</code>: computes the determinant of an matrix.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.eig(a)</code>: computes the eigenvalues and right eigenvectors of a square matrix.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.matrix_power(a, n)</code>: raises a square matrix to the (integer) power n.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.solve(a, b)</code>: Solve a linear matrix equation, or system of linear scalar equations. <code>a</code> should be square.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.inv(a)</code>: computes the (multiplicative) inverse of a matrix.</p> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p>Also requires a non-singular matrix (a matrix is singular if its determinant is 0).</p> | ||
</li> | ||
</ul> | ||
</div> | ||
</li> | ||
</ul> | ||
</div> | ||
<div class="paragraph"> | ||
<p>You can use <code>numpy.shape()</code> function to check the dimensions of the matrix before performing the operation.</p> | ||
</div> | ||
<div class="sect2"> | ||
<h3 id="_code_examples">Code examples</h3> | ||
<div class="sect3"> | ||
<h4 id="_noncompliant_code_example">Noncompliant code example</h4> | ||
<div class="listingblock"> | ||
<div class="content"> | ||
<pre class="highlight"><code class="language-python" data-lang="python">import numpy as np | ||
|
||
a = np.array([[1, 2], [3, 4], [5, 6]]) | ||
b = np.linalg.inv(a) # Noncompliant: LinAlgError: Last 2 dimensions of the array must be square</code></pre> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect3"> | ||
<h4 id="_compliant_solution">Compliant solution</h4> | ||
<div class="listingblock"> | ||
<div class="content"> | ||
<pre class="highlight"><code class="language-python" data-lang="python">import numpy as np | ||
|
||
a = np.array([[1, 2], [3, 4]]) | ||
b = np.linalg.inv(a) # Compliant</code></pre> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_resources">Resources</h2> | ||
<div class="sectionbody"> | ||
<div class="sect2"> | ||
<h3 id="_documentation">Documentation</h3> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.det.html">numpy.linalg.det</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html">numpy.linalg.eig</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.inv.html">numpy.linalg.inv</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_power.html">numpy.linalg.matrix_power</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.solve.html">numpy.linalg.solve</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.shape.html">numpy.shape</a></p> | ||
</li> | ||
</ul> | ||
</div> | ||
<hr> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_implementation_specification">Implementation Specification</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>(visible only on this page)</p> | ||
</div> | ||
<hr> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_comments_and_links">Comments And Links</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>(visible only on this page)</p> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"title":"Square matrices should be provided to NumPy functions expecting them","type":"BUG","code":{"impacts":{"RELIABILITY":"HIGH"},"attribute":"LOGICAL"},"status":"ready","remediation":{"func":"Constant/Issue","constantCost":"2min"},"tags":["data-science","numpy"],"extra":{"replacementRules":[]},"defaultSeverity":"Blocker","ruleSpecification":"RSPEC-6728","sqKey":"S6728","scope":"Main","defaultQualityProfiles":["Sonar way"],"quickfix":"unknown","allKeys":["S6728"],"prUrl":"https://github.com/SonarSource/rspec/pull/2958","branch":"rule/add-RSPEC-S6728","languagesSupport":[{"name":"python","status":"ready"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<div class="sect1"> | ||
<h2 id="_description">Description</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>This rule raises an issue when a NumPy function expecting a square matrix is called with a non-square matrix.</p> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>Some matrix operations only make sense on square matrices. For example, a determinant can be computed only if a matrix is square.</p> | ||
</div> | ||
<div class="paragraph"> | ||
<p>Therefore, NumPy functions performing these operations should be provided with a square matrix as an argument. If something other than a square matrix is provided, the computation does not make sense and an exception will be raised.</p> | ||
</div> | ||
<div class="paragraph"> | ||
<p>The following functions will raise an exception when provided with a non-square matrix:</p> | ||
</div> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p><code>np.linalg.det(a)</code>: computes the determinant of an matrix.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.eig(a)</code>: computes the eigenvalues and right eigenvectors of a square matrix.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.matrix_power(a, n)</code>: raises a square matrix to the (integer) power n.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.solve(a, b)</code>: Solve a linear matrix equation, or system of linear scalar equations. <code>a</code> should be square.</p> | ||
</li> | ||
<li> | ||
<p><code>np.linalg.inv(a)</code>: computes the (multiplicative) inverse of a matrix.</p> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p>Also requires a non-singular matrix (a matrix is singular if its determinant is 0).</p> | ||
</li> | ||
</ul> | ||
</div> | ||
</li> | ||
</ul> | ||
</div> | ||
<div class="paragraph"> | ||
<p>You can use <code>numpy.shape()</code> function to check the dimensions of the matrix before performing the operation.</p> | ||
</div> | ||
<div class="sect2"> | ||
<h3 id="_code_examples">Code examples</h3> | ||
<div class="sect3"> | ||
<h4 id="_noncompliant_code_example">Noncompliant code example</h4> | ||
<div class="listingblock"> | ||
<div class="content"> | ||
<pre class="highlight"><code class="language-python" data-lang="python">import numpy as np | ||
|
||
a = np.array([[1, 2], [3, 4], [5, 6]]) | ||
b = np.linalg.inv(a) # Noncompliant: LinAlgError: Last 2 dimensions of the array must be square</code></pre> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect3"> | ||
<h4 id="_compliant_solution">Compliant solution</h4> | ||
<div class="listingblock"> | ||
<div class="content"> | ||
<pre class="highlight"><code class="language-python" data-lang="python">import numpy as np | ||
|
||
a = np.array([[1, 2], [3, 4]]) | ||
b = np.linalg.inv(a) # Compliant</code></pre> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_resources">Resources</h2> | ||
<div class="sectionbody"> | ||
<div class="sect2"> | ||
<h3 id="_documentation">Documentation</h3> | ||
<div class="ulist"> | ||
<ul> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.det.html">numpy.linalg.det</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html">numpy.linalg.eig</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.inv.html">numpy.linalg.inv</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_power.html">numpy.linalg.matrix_power</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.linalg.solve.html">numpy.linalg.solve</a></p> | ||
</li> | ||
<li> | ||
<p>NumPy Documentation - <a href="https://numpy.org/doc/stable/reference/generated/numpy.shape.html">numpy.shape</a></p> | ||
</li> | ||
</ul> | ||
</div> | ||
<hr> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_implementation_specification">Implementation Specification</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>(visible only on this page)</p> | ||
</div> | ||
<hr> | ||
</div> | ||
</div> | ||
<div class="sect1"> | ||
<h2 id="_comments_and_links">Comments And Links</h2> | ||
<div class="sectionbody"> | ||
<div class="paragraph"> | ||
<p>(visible only on this page)</p> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"title":"Square matrices should be provided to NumPy functions expecting them","type":"BUG","code":{"impacts":{"RELIABILITY":"HIGH"},"attribute":"LOGICAL"},"status":"ready","remediation":{"func":"Constant/Issue","constantCost":"2min"},"tags":["data-science","numpy"],"extra":{"replacementRules":[]},"defaultSeverity":"Blocker","ruleSpecification":"RSPEC-6728","sqKey":"S6728","scope":"Main","defaultQualityProfiles":["Sonar way"],"quickfix":"unknown","allKeys":["S6728"],"prUrl":"https://github.com/SonarSource/rspec/pull/2958","branch":"rule/add-RSPEC-S6728","languagesSupport":[{"name":"python","status":"ready"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"langs":{"abap":123,"apex":70,"cfamily":923,"csharp":598,"default":3726,"flex":100,"go":62,"java":988,"javascript":577,"jcl":21,"kotlin":158,"php":329,"python":433,"rpg":94,"ruby":42,"scala":43,"swift":149,"plsql":216,"dart":115,"vbnet":266,"html":106,"pli":34,"tsql":86,"vb6":67,"xml":60,"cobol":225,"css":28,"ansible":17,"azureresourcemanager":32,"cloudformation":28,"docker":45,"kubernetes":24,"terraform":51,"secrets":126,"text":2},"tags":{"convention":233,"cert":364,"misra-c++2008":59,"pitfall":328,"based-on-misra":208,"cppcoreguidelines":86,"suspicious":189,"brain-overload":88,"misra":28,"bad-practice":140,"unused":84,"clumsy":181,"lock-in":49,"cwe":438,"error-handling":68,"psr2":8,"error-reporting":3,"user-experience":26,"sql":103,"obsolete":58,"android":29,"accessibility":48,"react":77,"wcag2-a":10,"injection":1,"html5":5,"cross-browser":4,"design":61,"bug":37,"confusing":148,"unpredictable":46,"es2015":22,"type-dependent":73,"redundant":25,"performance":195,"since-c++11":62,"multi-threading":55,"leak":15,"denial-of-service":18,"symbolic-execution":78,"data-science":25,"numpy":14,"pytorch":9,"microfocus":1,"logging":15,"jsp-jsf":6,"deprecated":12,"pep":6,"preprocessor":11,"java8":24,"tests":65,"junit":23,"phpunit":12,"security":5,"python3":18,"psr1":3,"per":2,"finding":12,"i18n":1,"serialization":18,"hibernate":9,"spring":56,"django":12,"express.js":17,"fastapi":2,"flask":6,"privacy":13,"api-design":31,"singleton":1,"AWS":1,"overflow":3,"jasmine":1,"jest":1,"mocha":4,"node":1,"struts":4,"misra-c2012":12,"backbone":3,"misra-c2004":19,"docker":2,"angularjs":1,"ejb":3,"jee":6,"chai":5,"syntax":4,"jquery":1,"assertj":7,"mockito":5,"msvc":1,"swing":1,"deadlock":3,"async-await":8,"php-ini":6,"typing":22,"maven":9,"since-c++14":5,"c11":1,"gnu":3,"xsd":18,"defign":1,"localisation":4,"duplicate":1,"mef":3,"winforms":1,"event":1,"regex":40,"wpf":1,"xaml":1,"proficiency":5,"aws":33,"azure":24,"gcp":20,"rules":1,"antipattern":1,"jsx":8,"yield":1,"debug":1,"java7":1,"guava":1,"java9":2,"full-project":3,"ssl":2,"phishing":1,"java14":6,"dockerfile":11,"governor-limits":5,"since-c++17":24,"unittest":2,"java15":1,"since-c++20":65,"java16":12,"java10":1,"java17":1,"startup-time":2,"coroutines":12,"cwe-284":1,"javadoc":12,"java18":9,"floating-point":1,"numbers":1,"precision":1,"architecture":9,"async":1,"promise":1,"sans-top25-insecure":1,"enum":2,"object":1,"string":1,"shell":1,"assertion":1,"function":1,"type":1,"es2020":1,"nullish-coalescing":1,"rspec-showcase":3,"Gradle":2,"gradle":10,"es2022":1,"es2018":1,"jspecify":1,"nullability":1,"scientific-computing":4,"pandas":9,"graphql":2,"blazor":9,"paths":1,"best-practice":1,"java21":15,"datetime":7,"sustainability":10,"java":1,"jpa":1,"machine-learning":18,"tensorflow":7,"susceptibility":1,"asp.net":11,"scikit-learn":5,"since-c++23":6,"respectful":1,"pyspark":6,"assembler":1},"qualityProfiles":{"Sonar way":2544,"Drupal":1}} | ||
{"langs":{"abap":123,"apex":70,"cfamily":923,"csharp":598,"default":3727,"flex":100,"go":62,"java":988,"javascript":577,"jcl":21,"kotlin":158,"php":329,"python":434,"rpg":94,"ruby":42,"scala":43,"swift":149,"plsql":216,"dart":115,"vbnet":266,"html":106,"pli":34,"tsql":86,"vb6":67,"xml":60,"cobol":225,"css":28,"ansible":17,"azureresourcemanager":32,"cloudformation":28,"docker":45,"kubernetes":24,"terraform":51,"secrets":126,"text":2},"tags":{"convention":233,"cert":364,"misra-c++2008":59,"pitfall":328,"based-on-misra":208,"cppcoreguidelines":86,"suspicious":189,"brain-overload":88,"misra":28,"bad-practice":140,"unused":84,"clumsy":181,"lock-in":49,"cwe":438,"error-handling":68,"psr2":8,"error-reporting":3,"user-experience":26,"sql":103,"obsolete":58,"android":29,"accessibility":48,"react":77,"wcag2-a":10,"injection":1,"html5":5,"cross-browser":4,"design":61,"bug":37,"confusing":148,"unpredictable":46,"es2015":22,"type-dependent":73,"redundant":25,"performance":195,"since-c++11":62,"multi-threading":55,"leak":15,"denial-of-service":18,"symbolic-execution":78,"data-science":26,"numpy":15,"pytorch":9,"microfocus":1,"logging":15,"jsp-jsf":6,"deprecated":12,"pep":6,"preprocessor":11,"java8":24,"tests":65,"junit":23,"phpunit":12,"security":5,"python3":18,"psr1":3,"per":2,"finding":12,"i18n":1,"serialization":18,"hibernate":9,"spring":56,"django":12,"express.js":17,"fastapi":2,"flask":6,"privacy":13,"api-design":31,"singleton":1,"AWS":1,"overflow":3,"jasmine":1,"jest":1,"mocha":4,"node":1,"struts":4,"misra-c2012":12,"backbone":3,"misra-c2004":19,"docker":2,"angularjs":1,"ejb":3,"jee":6,"chai":5,"syntax":4,"jquery":1,"assertj":7,"mockito":5,"msvc":1,"swing":1,"deadlock":3,"async-await":8,"php-ini":6,"typing":22,"maven":9,"since-c++14":5,"c11":1,"gnu":3,"xsd":18,"defign":1,"localisation":4,"duplicate":1,"mef":3,"winforms":1,"event":1,"regex":40,"wpf":1,"xaml":1,"proficiency":5,"aws":33,"azure":24,"gcp":20,"rules":1,"antipattern":1,"jsx":8,"yield":1,"debug":1,"java7":1,"guava":1,"java9":2,"full-project":3,"ssl":2,"phishing":1,"java14":6,"dockerfile":11,"governor-limits":5,"since-c++17":24,"unittest":2,"java15":1,"since-c++20":65,"java16":12,"java10":1,"java17":1,"startup-time":2,"coroutines":12,"cwe-284":1,"javadoc":12,"java18":9,"floating-point":1,"numbers":1,"precision":1,"architecture":9,"async":1,"promise":1,"sans-top25-insecure":1,"enum":2,"object":1,"string":1,"shell":1,"assertion":1,"function":1,"type":1,"es2020":1,"nullish-coalescing":1,"rspec-showcase":3,"Gradle":2,"gradle":10,"es2022":1,"es2018":1,"jspecify":1,"nullability":1,"scientific-computing":4,"pandas":9,"graphql":2,"blazor":9,"paths":1,"best-practice":1,"java21":15,"datetime":7,"sustainability":10,"java":1,"jpa":1,"machine-learning":18,"tensorflow":7,"susceptibility":1,"asp.net":11,"scikit-learn":5,"since-c++23":6,"respectful":1,"pyspark":6,"assembler":1},"qualityProfiles":{"Sonar way":2545,"Drupal":1}} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.