C# Three-Dimensional Cartesian Vector Calculator (c) September 24, 2023, by James Pate Williams, Jr. All Applicable Rights Reserved

I wrote and debugged this C# code after I found out that my 1989 vector calculator in Modula-2 for the Commadore Amiga 2000 was not working correctly.

// C# Three-Dimensional Cartesian Vector Calculator
// (c) September 24, 2023 by James Pate Williams, Jr.
// All Applicable Rights Reserved

using System;
using System.Windows.Forms;

namespace CSVectorCalculator
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private static double[] A = new double[3];
        private static double[] B = new double[3];
        private static double[] C = new double[3];

        private void ValidateAB(
            ref bool valid)
        {
            try
            {
                A[0] = double.Parse(textBox1.Text);
                A[1] = double.Parse(textBox2.Text);
                A[2] = double.Parse(textBox3.Text);
                B[0] = double.Parse(textBox4.Text);
                B[1] = double.Parse(textBox5.Text);
                B[2] = double.Parse(textBox6.Text);
                
                valid = true;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                valid = false;
            }
        }

        private void FillC(double[] C)
        {
            textBox7.Text = C[0].ToString();
            textBox8.Text = C[1].ToString();
            textBox9.Text = C[2].ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool valid = true;

            ValidateAB(ref valid);

            if (valid)
            {
                C[0] = A[0] + B[0];
                C[1] = A[1] + B[1];
                C[2] = A[2] + B[2];

                FillC(C);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool valid = true;
            
            ValidateAB(ref valid);

            if (valid)
            {
                C[0] = A[0] - B[0];
                C[1] = A[1] - B[1];
                C[2] = A[2] - B[2];

                FillC(C);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool valid = true;

            ValidateAB(ref valid);

            if (valid)
            {
                C[0] = A[1] * B[2] - A[2] * B[1];
                C[1] = A[0] * B[2] - A[2] * B[0];
                C[2] = A[1] * B[0] - A[0] * B[1];

                FillC(C);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bool valid = true;
 
            ValidateAB(ref valid);

            if (valid)
            {
                C[0] = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
                C[1] = C[2] = 0.0;

                FillC(C);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            bool valid = true;

            ValidateAB(ref valid);

            if (valid)
            {
                C[0] = Math.Sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
                C[1] = C[2] = 0.0;

                FillC(C);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool valid = true;

            ValidateAB(ref valid);

            if (valid)
            {
                textBox1.Text = C[0].ToString();
                textBox2.Text = C[1].ToString();
                textBox3.Text = C[2].ToString();

                C[0] = C[1] = C[2] = 0.0;

                FillC(C);
            }
        }
    }
}

Comparison of Linear Systems Applications in C# and C++ by James Pate Williams, Jr.

Back in 2017 I created a C# application that implemented the direct methods: Cholesky decomposition, Gaussian elimination with partial pivoting, LU decomposition, and simple Gaussian elimination. The classical iterative methods Gauss-Seidel, Jacobi, Successive Overrelaxation, and gradient descent were also implemented along with the modern iterative methods: conjugate gradient descent and Modified Richardson’s method. Recently I translated the C# code to C++. I used the following test matrices: Cauchy, Lehmer, Pascal, and other. Below are some results. As is apparent the C++ runtimes are faster than the C# execution times.

Richardson Method Translated from C Source Code to C# by James Pate Williams, Jr.

The Richardson Method is called before eliminating the system of linear equations.

Added elimination results from a vanilla C program and a C# application.

These results are not in total agreement with H. T. Lau’s results.