RvsZ3

From ZCubes Wiki
Revision as of 05:41, 8 July 2021 by Swapna (talk | contribs)
Jump to navigation Jump to search

Comparison of R vs Z3

Simple manipulations; numbers and vectors

Vectors and assignment

Z3 operates on named data structures. The simplest such structure is the numeric vector, which is a single entity consisting of an ordered collection of numbers. 

To set up a vector named x, say, consisting of five numbers, namely 10.4, 5.6, 3.1, 6.4 and 21.7, use the R command

> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)

Z3 command to set up a vector is:

x<==[10.4, 5.6, 3.1, 6.4, 21.7];

Alternatively we can use the simple "=" also.

 x=[10.4, 5.6, 3.1, 6.4, 21.7]

Assignment can also be made using the function ASSIGN(). An equivalent way of making the same assignment as above is with: In R,

> assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))

In Z3, use the "ASSIGN" function as:

ASSIGN("x", [10.4, 5.6, 3.1, 6.4, 21.7])

Assignments can also be made in the other direction, using the obvious change in the assignment operator. So the same assignment could be made using

[10.4, 5.6, 3.1, 6.4, 21.7]==>x 

The reciprocals of the above five values for x in R,

> 1/x

In Z3, We can use the function called Reciprocal,

RECIPROCAL(x) (the value of x is [10.4, 5.6, 3.1, 6.4, 21.7] 

Also we can use directly,

([10.4,5.6,3.1,6.4,21.7]<>d40)@(x=>1/x)

The further assignment

> y <- c(x, 0, x)

would create a vector y with 11 entries consisting of two copies of x with a zero in the middle place.

Vector arithmetic

Vectors can be used in arithmetic expressions, in which case the operations are performed element by element. Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression. So with the above assignments the R command

> v <- 2*x + y + 1

generates a new vector v of length 11 constructed by adding together, element by element, 2*x repeated 2.2 times, y repeated just once, and 1 repeated 11 times.

With the same Assignment Z3 command is.

v=2*x+y+1

In Z3 the elementary arithmetic operators are the usual +, -, *, / and ^ for raising to a power. Also we can use the functions SUM,SUB,PRODUCT,DIVIDE and POWER instead of using arithmetic operators.

In addition all of the common arithmetic functions are available. LOG, EXP, SQRT, SIN, COS, TAN, SEC,COSEC,COTAN, Hyperbolic functions and so on. For trignometric functions we can find the values in Deg and Radians also.

MAX and MIN select the largest and smallest elements of a vector respectively.

In R,two statistical functions are mean(x) which calculates the sample mean, which is the same as sum(x)/length(x), and var(x) which gives

sum((x-mean(x))^2)/(length(x)-1)

or sample variance.

In Z3,to find the mean value we can use function called MEAN(x), AVG(x) or AVERAGE(x).

In R, sort(x) returns a vector of the same size as x with the elements arranged in increasing order.

SORTING(x) returns the vector in increasing order in Z3.
      • Is there any other functions like order or list.

To work with complex numbers, supply an explicit complex part. Thus sqrt(-17) will give NaN and a warning, but sqrt(-17+0i) will do the computations as complex numbers in R.

In Z3, while computing complex numbers simply we can use as

SQRT(-17) or SQRT(-17+0i).
      • Need to know any functions equivalent to The parallel maximum and minimum functions pmax and pmin return a vector.

Generating regular sequences

The function seq() is a more general facility for generating sequences. To get the sequence of values from a particular range with step value with R as > seq(-5, 5, by=.2) -> s3 generates in s3 the vector c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0). Similarly > s4 <- seq(length=51, from=-5, by=.2) generates the same vector in s4.

Z3 command to get a sequence value as:

s3=-5..5..0.2

generates in s3 vector.

s4=Array(51).seq(-5,0.2)

generates s4 vector with same as s3.

To print the five copies of x ene-to-end in s5 the R command is > s5 <- rep(x, times=5) Alternatively > s6 <- rep(x, each=5) which repeats each element of x five times before moving on to the next.

The z3 command of replicating the array value is: s5=x.replicate(5)

The another way is: s6=RECURSIVEARRAY(5,x)

Logical vectors

Objects, their modes and attributes

Intrinsic attributes: mode and length

R consists of a number of data objects to perform various functions. There are 6 types of objects in R Programming. They include vector, list, matrix, array, factor, and data frame.

Vectors in R programming data objects: logical, integer, character, raw, double, and complex.

Z3 language also supports data objects: logical, integer, character, raw, double, and complex.


Lists in R contain various types of elements including strings, numbers, vectors, and a nested list inside it. It can also consist of matrices or functions as elements. It can be created with the help of the list() function.

Z3 stores all the data in array format. The data can be strings, numbers, vectors, matrices or functions as elements.

List of elements can be displayed using Z3 command LISTALL.


Matrices in R Programming are used to arrange elements in the two-dimensional layout to perform mathematical operations.

Matrices in Z3 can be of any dimensions. A matrix can be defined in many ways such as:

MATRIX(3)     //Displays 3x3 matrix

or

MATRIX("anti-diagonal",4,200..204)  //Displays 4x4 anti-diagonal matrix with values inbetween 200 and 204

or

|5|   //Displays 5x5 matrix

or

|2,3,4|   //Displays 2x3x4 matrix


An array in R is used to store data in multi-dimensional format. It can be created with the help of an array() function.

Z3 has n number of commands for using array functions such as:

ARRAY(3,4)    //Defines a 3-dimensional array with each element value of 4
a=[[1,3,4],[2,3,4]]		//Defines an array 'a'
a.add(45)			//Adds 45 in each array element

Row, Column, Diagonal, concatination etc operations are possible using Z3 commands. (Refer list of Array Manipulation Functions here: https://wiki.zcubes.com/Z%5E3_Array_Manipulation_Member_Functions)


Factors are data objects that are used in order to categorize and store data as levels. They can be strings or integers. They are extremely useful in data analytics for statistical modeling. They can be created using factor() function.

Factors can be identified or retrieved in Z3 by giving variable name as a command.

a=[[11,3,4],[21,3,4]]		//Defines array 'a'
a				//Displays elements of array 'a'

Dataframe is a 2-dimensional data structure wherein each column consists of the value of one variable and each row consists of a value set from each column.

.................Need to add explanation for this..............................


Arrays and Matrices

Arrays

A 3 by 5 by 100 dimension vector z of 1500 elements is defined with R command as:

> dim(z) <- c(3,5,100)

Z3 command to define an array is:

DIM(3,5,100)

Alternatively it can also be represented in array form as:

|3,5,100|


Array indexing. Subsections of an array

A 4 x 2 array with array elements is represented by R command as:

c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1],
a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2])

In Z3, the above 4 X 2 array with array elements is defined using square brackets as:

[[2,1,1], [2,2,1], [2,3,1], [2,4,1], [2,1,2], [2,2,2], [2,3,2], [2,4,2]]

The above array can be stored with a variable name 'Z' as:

z = [[2,1,1], [2,2,1], [2,3,1], [2,4,1], [2,1,2], [2,2,2], [2,3,2], [2,4,2]]

The contents of variable Z can be obtained using Z3 command:

DIM(z)

Also, to identify the size of 'z', use the Z3 command:

DIMENSIONS(z) 

which gives the result as: 8 3 (8 rows, 3 columns)

Index matrices

A matrix 'x' with 4 rows and 5 colums containing values from 1 to 20, is defined using R command as:

> x <- array(1:20, dim=c(4,5))

This command displays the result as:

    [,1] [,2] [,3] [,4] [,5]
   
[1,] 1    5    9    13   17
[2,] 2    6   10    14   18
[3,] 3    7   11    15   19
[4,] 4    8   12    16   20


The Z3 command can be used as:

x= |4,5,1..20|

In Z3, the array elements are stroed row wise. Hence using a TRANSPOSE command will result into the same output as above.

TRANSPOSE(x) 




Please check back in couple of days. We are updating the page.