{"_id":"566e55c12c1e760d0030c573","__v":0,"category":{"_id":"566e55c12c1e760d0030c56d","__v":2,"pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-17T03:34:44.886Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"user":"544083bee239230800071bef","project":"54408e54309354080070a896","version":{"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","__v":1,"createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","categories":["566e55c12c1e760d0030c56d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.3","version":"1.2.3"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-11-13T18:36:04.225Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":5,"body":"# Introduction\n\nNode also supports native arrays and *Buffer* objects; these are 1 dimensional arrays who's elements are equivalent to the Julia's concept of a native type.  These arrays come with the cost of some loss of flexibility because they are of one element type and can not be increased in size, however for that they they gain an efficiency of about 10x speed.  \n\nBesides being declared differently than normal arrays, native arrays are used largely in the same way.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var a = new Float64Array(1000);\\n\\nfor(var i = 0;i < a.length;i++) a[i] = i;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nAn application can gain a large speed advantage if it can can manage around the loss of flexibility.  Many applications where Julia is used consist of high speed calculations on increments of a a set size of data and so can meet this criterion.  Thus, in version 0.2.1, the datatype mapping support has been extended to include these types as well.\n\n## Native Array Mapping\n\nJavascript native arrays are mapped to the following:\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Javascript Type\",\n    \"h-1\": \"Julia Type\",\n    \"0-0\": \"Int8Array\",\n    \"1-0\": \"Uint8Array\",\n    \"2-0\": \"Int16Array\",\n    \"3-0\": \"Uint16Array\",\n    \"4-0\": \"Int32Array\",\n    \"5-0\": \"Uint32Array\",\n    \"6-0\": \"Float32Array\",\n    \"7-0\": \"Float64Array\",\n    \"8-0\": \"Buffer\",\n    \"0-1\": \"Array{Int8,1}\",\n    \"1-1\": \"Array{Uint8,1}\",\n    \"2-1\": \"Array{Int16,1}\",\n    \"3-1\": \"Array{Uint16,1}\",\n    \"4-1\": \"Array{Int32,1}\",\n    \"5-1\": \"Array{Uint32,1}\",\n    \"6-1\": \"Array{Float32,1}\",\n    \"7-1\": \"Array{Float64,1}\",\n    \"8-1\": \"Array{Uint8,1}\"\n  },\n  \"cols\": 2,\n  \"rows\": 9\n}\n[/block]\n## NativeArrays are used whenever possible\n\nThe cases above are further extended to include multidimensional arrays.  So long as the element type is numeric then NativeArrays will be used as defined below.   A Julia array of *N* dimensions will be mapped to JavaScript Array of *N-1* dimensions where the elements of the rightmost dimension will be NativeArrays.  The following table gives the element type mapping. \n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Julia Element Type\",\n    \"h-1\": \"JavaScript NativeArray\",\n    \"0-0\": \"Float64\",\n    \"0-1\": \"Float64Array\",\n    \"1-0\": \"Float32\",\n    \"1-1\": \"Float32Array\",\n    \"2-0\": \"Int64\",\n    \"2-1\": \"Float64Array*\",\n    \"3-0\": \"UInt64\",\n    \"3-1\": \"Float64Array*\",\n    \"4-0\": \"Int32\",\n    \"4-1\": \"Int32Array\",\n    \"5-0\": \"UInt32\",\n    \"5-1\": \"Uint32Array\",\n    \"6-0\": \"Int16\",\n    \"6-1\": \"Uint16Array\",\n    \"7-0\": \"UInt16\",\n    \"7-1\": \"Uint16Array\",\n    \"8-0\": \"Int8\",\n    \"8-1\": \"Int8Array\",\n    \"9-0\": \"UInt8\",\n    \"9-1\": \"UIntArray**\"\n  },\n  \"cols\": 2,\n  \"rows\": 10\n}\n[/block]\nThe following example illustrates the creation of these arrays\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\n\\nvar a = julia.eval('[i for i = 1:8]');\\nvar r = julia.exec('reshape',a,2,2,2);\\n\\nconsole.log(\\\"Array a: \\\",a);\\nconsole.log(\\\"Array r:\\\");\\nfor(var i = 0;i < 2;i++) {\\n   for(var j = 0;j < 2;j++) {\\n      console.log('[' + r[i][j][0] + ', ' + r[i][j][1] + ']');\\n   }\\n   console.log('');\\n}\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nwhich produces the following output:\n\n    Array a:  { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6, '6': 7, '7': 8 }\n    Array r:\n    [1, 5]\n    [3, 7]\n\n    [2, 6]\n    [4, 8]\n\n### Arrays of Native Arrays mapped to Julia multidimensional arrays\nTo facilitate using the output of the mapping above as arguments for Julia functions, JavaScript arrays of dimension *N* where the element type are NativeArrays are mapped to Julia arrays of dimension *N + 1* of the appropriate type.  The following illustrates the possibilities\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\n\\nvar e1 = new Float64Array(2);\\nvar e2 = new Float64Array(2);\\nvar a = [e1,e2];\\n\\ne1[0] = e2[1] = 1;\\ne1[1] = e2[0] = 2;\\n\\nvar aInv = julia.exec('inv',a);\\nvar b = julia.exec('*',a,aInv);\\n\\nconsole.log('a times a inverse (should be identity matrix)');\\nfor(var i = 0;i < 2;i++) {\\n   console.log('[' + b[i][0] + ', ' + b[i][1] + ']');\\n}\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nwhich produces\n\n    a times a inverse (should be identity matrix)\n    [1, 0]\n    [0, 1]\n\n### Element type Int64 mapped to Float64Array*\nIn JavaScript, there is no Int64Array type due to Number being implicitly a double value. Thus the closest approximation is Float64Array.\n \n### The special case of Buffer**\nBecause Javascript **Buffer** objects are closely tied to native arrays of *Uint8*, the Julia type Array{Uint8,1} when it appears as a single result and not as part of a multidimensional array will be returned as a JavaScript Buffer.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Julia Array Type\",\n    \"h-1\": \"Javascript Type\",\n    \"0-0\": \"Array{Uint8,1}\",\n    \"0-1\": \"Buffer\"\n  },\n  \"cols\": 2,\n  \"rows\": 1\n}\n[/block]\nThis transformation only applies if the dimension of the source array is 1 for example if a buffer of 16 elements is created and returned with the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var a = new Buffer(16);\\n\\nfor(var i = 0;i < a.length;i++) a[i] = i;\\n\\nconsole.log(julia.exec('identity',a));\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nthis produces the following:\n\n    <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f>\n    \nwhile if instead the dimensions are changed to 4x4 using the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var a = new Buffer(16);\\n\\nfor(var i = 0;i < a.length;i++) a[i] = i;\\n\\nconsole.log(julia.exec('reshape',a,4,4));\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nthis produces the following (an array of 4 UInt8Arrays instead of Buffer):\n\n    [ { '0': 0, '1': 4, '2': 8, '3': 12 },\n      { '0': 1, '1': 5, '2': 9, '3': 13 },\n      { '0': 2, '1': 6, '2': 10, '3': 14 },\n      { '0': 3, '1': 7, '2': 11, '3': 15 } ]","excerpt":"","slug":"buffer-and-native-arrays","type":"basic","title":"Buffer and Native Arrays"}

Buffer and Native Arrays


# Introduction Node also supports native arrays and *Buffer* objects; these are 1 dimensional arrays who's elements are equivalent to the Julia's concept of a native type. These arrays come with the cost of some loss of flexibility because they are of one element type and can not be increased in size, however for that they they gain an efficiency of about 10x speed. Besides being declared differently than normal arrays, native arrays are used largely in the same way. [block:code] { "codes": [ { "code": "var a = new Float64Array(1000);\n\nfor(var i = 0;i < a.length;i++) a[i] = i;", "language": "javascript" } ] } [/block] An application can gain a large speed advantage if it can can manage around the loss of flexibility. Many applications where Julia is used consist of high speed calculations on increments of a a set size of data and so can meet this criterion. Thus, in version 0.2.1, the datatype mapping support has been extended to include these types as well. ## Native Array Mapping Javascript native arrays are mapped to the following: [block:parameters] { "data": { "h-0": "Javascript Type", "h-1": "Julia Type", "0-0": "Int8Array", "1-0": "Uint8Array", "2-0": "Int16Array", "3-0": "Uint16Array", "4-0": "Int32Array", "5-0": "Uint32Array", "6-0": "Float32Array", "7-0": "Float64Array", "8-0": "Buffer", "0-1": "Array{Int8,1}", "1-1": "Array{Uint8,1}", "2-1": "Array{Int16,1}", "3-1": "Array{Uint16,1}", "4-1": "Array{Int32,1}", "5-1": "Array{Uint32,1}", "6-1": "Array{Float32,1}", "7-1": "Array{Float64,1}", "8-1": "Array{Uint8,1}" }, "cols": 2, "rows": 9 } [/block] ## NativeArrays are used whenever possible The cases above are further extended to include multidimensional arrays. So long as the element type is numeric then NativeArrays will be used as defined below. A Julia array of *N* dimensions will be mapped to JavaScript Array of *N-1* dimensions where the elements of the rightmost dimension will be NativeArrays. The following table gives the element type mapping. [block:parameters] { "data": { "h-0": "Julia Element Type", "h-1": "JavaScript NativeArray", "0-0": "Float64", "0-1": "Float64Array", "1-0": "Float32", "1-1": "Float32Array", "2-0": "Int64", "2-1": "Float64Array*", "3-0": "UInt64", "3-1": "Float64Array*", "4-0": "Int32", "4-1": "Int32Array", "5-0": "UInt32", "5-1": "Uint32Array", "6-0": "Int16", "6-1": "Uint16Array", "7-0": "UInt16", "7-1": "Uint16Array", "8-0": "Int8", "8-1": "Int8Array", "9-0": "UInt8", "9-1": "UIntArray**" }, "cols": 2, "rows": 10 } [/block] The following example illustrates the creation of these arrays [block:code] { "codes": [ { "code": "var julia = require('node-julia');\n\nvar a = julia.eval('[i for i = 1:8]');\nvar r = julia.exec('reshape',a,2,2,2);\n\nconsole.log(\"Array a: \",a);\nconsole.log(\"Array r:\");\nfor(var i = 0;i < 2;i++) {\n for(var j = 0;j < 2;j++) {\n console.log('[' + r[i][j][0] + ', ' + r[i][j][1] + ']');\n }\n console.log('');\n}\n", "language": "javascript" } ] } [/block] which produces the following output: Array a: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6, '6': 7, '7': 8 } Array r: [1, 5] [3, 7] [2, 6] [4, 8] ### Arrays of Native Arrays mapped to Julia multidimensional arrays To facilitate using the output of the mapping above as arguments for Julia functions, JavaScript arrays of dimension *N* where the element type are NativeArrays are mapped to Julia arrays of dimension *N + 1* of the appropriate type. The following illustrates the possibilities [block:code] { "codes": [ { "code": "var julia = require('node-julia');\n\nvar e1 = new Float64Array(2);\nvar e2 = new Float64Array(2);\nvar a = [e1,e2];\n\ne1[0] = e2[1] = 1;\ne1[1] = e2[0] = 2;\n\nvar aInv = julia.exec('inv',a);\nvar b = julia.exec('*',a,aInv);\n\nconsole.log('a times a inverse (should be identity matrix)');\nfor(var i = 0;i < 2;i++) {\n console.log('[' + b[i][0] + ', ' + b[i][1] + ']');\n}\n", "language": "javascript" } ] } [/block] which produces a times a inverse (should be identity matrix) [1, 0] [0, 1] ### Element type Int64 mapped to Float64Array* In JavaScript, there is no Int64Array type due to Number being implicitly a double value. Thus the closest approximation is Float64Array. ### The special case of Buffer** Because Javascript **Buffer** objects are closely tied to native arrays of *Uint8*, the Julia type Array{Uint8,1} when it appears as a single result and not as part of a multidimensional array will be returned as a JavaScript Buffer. [block:parameters] { "data": { "h-0": "Julia Array Type", "h-1": "Javascript Type", "0-0": "Array{Uint8,1}", "0-1": "Buffer" }, "cols": 2, "rows": 1 } [/block] This transformation only applies if the dimension of the source array is 1 for example if a buffer of 16 elements is created and returned with the following: [block:code] { "codes": [ { "code": "var a = new Buffer(16);\n\nfor(var i = 0;i < a.length;i++) a[i] = i;\n\nconsole.log(julia.exec('identity',a));", "language": "javascript" } ] } [/block] this produces the following: <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f> while if instead the dimensions are changed to 4x4 using the following: [block:code] { "codes": [ { "code": "var a = new Buffer(16);\n\nfor(var i = 0;i < a.length;i++) a[i] = i;\n\nconsole.log(julia.exec('reshape',a,4,4));", "language": "javascript" } ] } [/block] this produces the following (an array of 4 UInt8Arrays instead of Buffer): [ { '0': 0, '1': 4, '2': 8, '3': 12 }, { '0': 1, '1': 5, '2': 9, '3': 13 }, { '0': 2, '1': 6, '2': 10, '3': 14 }, { '0': 3, '1': 7, '2': 11, '3': 15 } ]