You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/04-refactoring.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -559,7 +559,7 @@ proc.time() - start_time
559
559
560
560
```
561
561
## user system elapsed
562
-
## 8.467 0.003 8.469
562
+
## 8.472 0.000 8.472
563
563
```
564
564
565
565
:::{.query}
@@ -587,7 +587,7 @@ proc.time() - start_time
587
587
588
588
```
589
589
## user system elapsed
590
-
## 0.716 0.575 0.628
590
+
## 0.744 0.600 0.646
591
591
```
592
592
593
593
The `outer()` function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The `rowSums()` function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index `i` in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.
@@ -615,7 +615,7 @@ proc.time() - start_time
615
615
616
616
```
617
617
## user system elapsed
618
-
## 0.310 0.291 0.255
618
+
## 0.364 0.259 0.262
619
619
```
620
620
621
621
One optimized way to perform the same calculation is by using the `%*%` operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.
<p>Overall, we hope that these guidelines and tips will help us all to use AI tools more responsibly for writing code. We recognize however, that as this is emerging technology and more ethical issues will emerge as we continue to use these tools in new ways. AI tools can even help us to use them more responsibly when we ask the right additional questions when writing code, but remember that human review is always necessary. Staying up-to-date on the current ethical considerations will also help us all continue to use AI responsibly.</p>
Copy file name to clipboardExpand all lines: docs/introduction.html
+10-2
Original file line number
Diff line number
Diff line change
@@ -486,11 +486,19 @@ <h2><span class="header-section-number">1.6</span> About This Book<a href="intro
486
486
<p>We used AI bots (including ChatGPT, Bard, Sage, and Claude) as part of the process of creating this book. The words are all original content, but we used AI bots to edit for grammar and tone. We firmly believe this new technology can be used in ethical ways and we put into practice all the rules and suggestions offered over the following modules while we created this book.</p>
<p>The <code>outer()</code> function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The <code>rowSums()</code> function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index <code>i</code> in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.</p>
785
785
</div>
786
786
<p>So much faster! We can look at another option by regenerating the response:</p>
<p>One optimized way to perform the same calculation is by using the <code>%*%</code> operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.</p>
804
804
</div>
805
805
<p>While this second suggestion is faster, you will need to consider what aspects of the codebase are most important in each instance. For example, this code runs more quickly, but <ahref="https://stat.ethz.ch/R-manual/R-patched/library/base/html/matmult.html">the <code>%*%</code> operator</a> might be unfamiliar to some R programmers. In cases where efficiency is less important, or the data are not large, you might consider maximizing readability.</p>
0 commit comments