Open In App

Tensorflow.js tf.complex() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js.

The tf.complex() function is used to convert two real numbers to a complex number.

Syntax:

tf.complex(real, imag)

Parameter:

  • real: It can be any of these three tf.Tensor, TypedArray, or Array. It represents the real part of the complex number formed.
  • imag: It can also be any of this three tf.Tensor, TypedArray, or Array. It represents the imaginary part of the complex number formed.

Return Value: It returns a tf.Tensor.

Note: The real parameter is the tf.Tensor represents the real part of the complex number and the imag parameter is the tf.Tensor represents the imaginary part of the complex number.
For example, 

  • Let real be [r0, r1, r2]
  • Let imag be [i0, i1, i2]
  • The converted complex number will be [r0 + i0, r1 + i1, r2 + i2]

Example 1:

Javascript




// The complex function containing the
// real and imag Typedarray
const complex = tf.complex ([5,3],[1,3]);
  
// Printing the tensor
complex.print();


Output:

Tensor
   [5 + 1j, 3 + 3j]

Example 2:

Javascript




// The complex function containing the
// real and imag Typedarray
const complex = tf.complex(
    tf.tensor1d([4.75, 5.75]),
    tf.tensor1d([2.25, 3.25])
);
  
// Printing the tensor
complex.print();


Output:

Tensor
   [4.75 + 2.25j, 5.75 + 3.25j]

Reference: https://js.tensorflow.org/api/latest/#complex


Last Updated : 20 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads