Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revisionBoth sides next revision
documentation:language_reference:objects:matrix:functions:touserdata [2018/09/25 13:26] – created Simon Heinzedocumentation:language_reference:objects:matrix:functions:touserdata [2018/09/25 15:34] – Added Code Example Simon Heinze
Line 5: Line 5:
 Matrices are, per default, tables of tables in Lua. When algebraic operations are performed on them, they are repeatedly copied and moved, which can lead to severe performance issues if the matrices are large objects. Matrices are, per default, tables of tables in Lua. When algebraic operations are performed on them, they are repeatedly copied and moved, which can lead to severe performance issues if the matrices are large objects.
  
-A call of Matrix.ToUserdata($M$) takes a matrix and transforms it, from Lua's point of view, from a table of tables to a mere pointer. This means that the object still has all functionalities Quanty defines on it with increased performance. The downside is that any functionalities Lua defines on tables is not necessarily longer available, most notably the element-wise access via the Bracket operator [].+A call of Matrix.ToUserdata($M$) takes a matrix and transforms it, from Lua's point of view, from a table of tables to a mere pointer, which it returns. This means that the object still has all functionalities Quanty defines on it with increased performance. The downside is that any functionalities Lua defines on tables is not necessarily longer available, most notably the element-wise access via the Bracket operator [].
  
 The inverse operation of Matrix.ToUserdata() is //[[documentation:language_reference:objects:matrix:functions:ToTable|Matrix.ToTable()]]//. The inverse operation of Matrix.ToUserdata() is //[[documentation:language_reference:objects:matrix:functions:ToTable|Matrix.ToTable()]]//.
Line 11: Line 11:
  
 ===== Example ===== ===== Example =====
- 
-### 
-Will follow. 
-### 
  
 ==== Input ==== ==== Input ====
 <code Quanty Example.Quanty> <code Quanty Example.Quanty>
--- some example code+--This creates a table of tables 
 +A = Matrix.Random({-2,2},{10000,10000}) 
 +setmetatable(A, MatrixMeta) 
 + 
 +TimeStart("Table of Tables"
 +B = Matrix.Transpose(A) 
 +B = Matrix.Conjugate(B) 
 +B = Matrix.ConjugateTranspose(B) 
 +B = B - A 
 +print(B[1][1]) 
 +TimeEnd("Table of Tables"
 + 
 +AUData = Matrix.ToUserdata(A) 
 +--A is, after this point, no longer needed. 
 +A = nil 
 +B = nil 
 +collectgarbage() 
 + 
 +TimeStart("Userdata"
 +B = Matrix.Transpose(AUData) 
 +B = Matrix.Conjugate(B) 
 +B = Matrix.ConjugateTranspose(B) 
 +B = B - AUData 
 +--In case we want to access the first element. 
 +B = Matrix.ToTable(B) 
 +print(B[1][1]) 
 +TimeEnd("Userdata"
 + 
 +TimePrint()
 </code> </code>
  
 ==== Result ==== ==== Result ====
 <file Quanty_Output> <file Quanty_Output>
-text produced as output+
 +
 +Timing results 
 +   Total_time | NumberOfRuns | Running | Name 
 +      0:00:13 |            1 |       0 | Table of Tables 
 +      0:00:02 |            1 |       0 | Userdata
 </file> </file>
  
Print/export