R Tutorial - References vs. Copies
The Nature of Big Matrices: Understanding References and Copies
Big matrices have been designed to look and feel like regular our matrix, allowing users to retrieve subsets of a big matrix just as they would with a regular one. Furthermore, users can set values for a big matrix using the same syntax as regular matrices. However, it's essential to recognize that big matrices are not actually part of the regular our matrix, leading to certain key differences.
One crucial distinction between big matrices and regular matrices is where they are stored in memory. Big matrix objects are typically stored on disk rather than RAM, which means they can persist across sessions and even be shared among them. This difference has significant implications for how big matrices behave when used within programs.
Another important aspect of big matrices is how assignments to variables work with them. Unlike regular matrices, where assignments create new copies of the matrix, big matrix objects do not make copies in this way. Instead, assignments create new variables that point to the same data structure as the original big matrix. This means that if a value within the big matrix needs to be updated, those changes will be reflected across all variables that reference it.
The behavior of big matrices when passed as function parameters is also noteworthy. When an assignment is made to a variable within a function, the result is not actually copied from the original value; rather, a new variable is created that points to the same data. This means that if changes are made to this new variable within the function, they will be reflected outside of the function as well. However, it's also possible to make copies of big matrices using the deep copy function.
To illustrate these concepts in practice, let's consider an example involving a big matrix called X. Assigning a new variable X no copy to X demonstrates how the changes made to X are reflected in X no copy, since both variables point to the same data structure. However, if we create X copy by explicitly applying the deep copy function on X, then X copy will have its own independent copy of the data, and any changes made to X will not be reflected in X copy.
In conclusion, it's essential for users of big matrices to understand their behavior, particularly when it comes to references and copies. By recognizing how these concepts work, users can effectively manipulate big matrix objects without inadvertently making unintended changes.
"WEBVTTKind: captionsLanguage: enas mentioned earlier big matrices have been designed to look and feel like regular our matrix you can retrieve subsets of a big matrix just like you would a regular our matrix you can also set values of a big matrix just like you would a regular our matrix however it is sometimes important to realize that a big matrix isn't actually in our matrix there are a couple of differences that can be important one difference which you already know is that a big matrix object is normally stored on the disk rather than Ram this means that a big matrix can persist across our sessions and can even be shared among our sessions now the difference is that the big matrix object is not copied like a regular matrix when an hour variable is assigned to other variables it actually gets a copy of the content this is also true when parameters are passed to functions here we assign the value 42 to the variable a next we create a function foo that takes the same a parameter sets it to 43 and then outputs its value what we call full of a the printed message tells us that a inside the function is 43 when we print the value of a outside the function it is still 42 this is because the function foo doesn't actually get a it gets a copy of a if we want to change the original a we need to do so outside of the function there are other types of variables like environments that are not copied when they are passed to a function for these types it is the actual data structure and as a result if you change their values inside a function you will see those changes after the function is finished executing those types of objects have reference semantics a big matrix is a reference object this means that assignments create new variables that point to the same data if you want a copy you need to do it explicitly using the deep copy function reference behavior ensures that our won't make copies of the matrix without you knowing this helps minimize memory usage and execution time but it also means you need to be careful when our making changes here we have an example that shows the reference behavior of a big matrix we start by creating a new big matrix called X we assign a new variable X no copy to X X no copy now reference is the same data as X if you update X the changes will be reflected it X no copy as well however when you create X copy by explicitly applying deep copy on X then X Copy has its own copy of the data in X so if you change a value in X it will not be reflected in X copy now that you have seen the differences between copies and references you get to spend a little time manipulating reference objectsas mentioned earlier big matrices have been designed to look and feel like regular our matrix you can retrieve subsets of a big matrix just like you would a regular our matrix you can also set values of a big matrix just like you would a regular our matrix however it is sometimes important to realize that a big matrix isn't actually in our matrix there are a couple of differences that can be important one difference which you already know is that a big matrix object is normally stored on the disk rather than Ram this means that a big matrix can persist across our sessions and can even be shared among our sessions now the difference is that the big matrix object is not copied like a regular matrix when an hour variable is assigned to other variables it actually gets a copy of the content this is also true when parameters are passed to functions here we assign the value 42 to the variable a next we create a function foo that takes the same a parameter sets it to 43 and then outputs its value what we call full of a the printed message tells us that a inside the function is 43 when we print the value of a outside the function it is still 42 this is because the function foo doesn't actually get a it gets a copy of a if we want to change the original a we need to do so outside of the function there are other types of variables like environments that are not copied when they are passed to a function for these types it is the actual data structure and as a result if you change their values inside a function you will see those changes after the function is finished executing those types of objects have reference semantics a big matrix is a reference object this means that assignments create new variables that point to the same data if you want a copy you need to do it explicitly using the deep copy function reference behavior ensures that our won't make copies of the matrix without you knowing this helps minimize memory usage and execution time but it also means you need to be careful when our making changes here we have an example that shows the reference behavior of a big matrix we start by creating a new big matrix called X we assign a new variable X no copy to X X no copy now reference is the same data as X if you update X the changes will be reflected it X no copy as well however when you create X copy by explicitly applying deep copy on X then X Copy has its own copy of the data in X so if you change a value in X it will not be reflected in X copy now that you have seen the differences between copies and references you get to spend a little time manipulating reference objects\n"