/// <summary> /// Divides a tensor by a scalar /// </summary> /// <param name="Input">The tensor to divide from</param> /// <param name="Scalar">The scalar to divide by</param> public static Tensor Tensor_Divide(Tensor Input, double Scalar) { //Create a flat tensor from the input and another one for the result FlatTensor Flat = FlatTensor.FromTensor(Input); FlatTensor Result = new FlatTensor(Flat.Count); //For each value in the flat tensor: for (int i = 0; i < Flat.Count; i++) { //Divide the value of the tensor by the scalar and add it to the result Result[i] = Flat[i] / Scalar; } //Return the result return Result.ToTensor(); }
/// <summary> /// Copies all of the values in a tensor to a new one. /// </summary> /// <param name="Input">The tensor to copy</param> public static Tensor Tensor_Copy(Tensor Input) { if (Input.Rank > 0) { //Create a flat tensor from the input and another one for the result FlatTensor Flat = FlatTensor.FromTensor(Input); FlatTensor Result = new FlatTensor(Flat.Count); //For each value in the flat tensor: for (int i = 0; i < Flat.Count; i++) { //Copy the value to the result Result[i] = Flat[i]; } //Return the result return Result.ToTensor(); } else { //We are working with a scalar Tensor ScalarTensor = new Tensor(0, 0); ScalarTensor.Data = (double)Input.Data; return ScalarTensor; } }
/// <summary> /// Negates all of the values of a tensor (flips the sign of each value in the tensor). /// </summary> /// <param name="Input">The tensor to negate</param> public static Tensor Tensor_Negate(Tensor Input) { //The best way to accomplish this, is to unfold the tensor, multiply each value by -1, and then fold the tensor FlatTensor Flat = FlatTensor.FromTensor(Input); FlatTensor Result = new FlatTensor(Flat.Count); //For each value in the flat tensor: for (int i = 0; i < Flat.Count; i++) { //Multiply the value by -1 and add it to the result Result[i] = Flat[i] * -1.0; } //Return the result return Result.ToTensor(); }
/// <summary> /// Computes the subtraction of two tensors of the same rank and underlying dimensionalities. /// </summary> /// <param name="Tensor1">The tensor to subtract from</param> /// <param name="Tensor2">The tensor to subtract</param> public static Tensor Tensor_Subtract(Tensor Tensor1, Tensor Tensor2) { //Only two tensors of the same rank may be subtracted if (Tensor1.Rank != Tensor2.Rank) throw new Exception("Tensors must be the same rank!"); //To subtract two tensors, first we want to unfold each into a flat tensor FlatTensor Flat1 = FlatTensor.FromTensor(Tensor1); FlatTensor Flat2 = FlatTensor.FromTensor(Tensor2); //Make sure the resulting flat tensors are the same length if (Flat1.Count != Flat2.Count) throw new Exception("Tensors must have the same underlying dimensionality at each sub-rank!"); //Now subtract them like we would two vectors FlatTensor Result = new FlatTensor(Flat1.Count); for (int i = 0; i < Flat1.Count; i++) { Result[i] = Flat1[i] - Flat2[i]; } //Finally, fold the resulting flat tensor back into a regular tensor return Result.ToTensor(); }