/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.text.*;
/*
 * NewJApplet.java
 *
 * Created on 10.03.2010, 09:25:36
 */

/**
 *
 * @author poj
 */
public class NewJApplet extends javax.swing.JApplet {

    int nmax = 100;
    int ndim = 5;
    int[] iindex;
    double[][] a, L, a0, R;
    double[] x, b, y, b0, bk, qb,deltar,uvec,aicol;
    double p, z;
    int i, j, k;
    DecimalFormat df, df2;
    int matrix = 1;
    double rx = 0;
    boolean reort=false;

    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();


        }

        a = new double[nmax][nmax];
        a0 = new double[nmax][nmax];
        R = new double[nmax][nmax];
        L = new double[nmax][nmax];
        x = new double[nmax];
        b = new double[nmax];
        b0 = new double[nmax];
        y = new double[nmax];
        bk = new double[nmax];
        qb = new double[nmax];
        deltar=new double[nmax];
        uvec = new double[nmax];
        aicol=new double[nmax];
        iindex = new int[nmax];
        df = new DecimalFormat("##0.000000");
        df2 = new DecimalFormat("##0.000000000000000");
        jSlider1.setValue(ndim);
        jLabel3.setText("dimension " + ndim);
        matrix=1;
       setmat();
       copymat();
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new myPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jSlider1 = new javax.swing.JSlider();
        jButton3 = new javax.swing.JButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Gauss");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Gauss  pivot");
        jButton2.setMaximumSize(new java.awt.Dimension(40, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(40, 25));
        jButton2.setPreferredSize(new java.awt.Dimension(40, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Hilbert");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("random");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setSelected(true);
        jRadioButton2.setText("well conditioned");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jSlider1.setMajorTickSpacing(1);
        jSlider1.setMaximum(60);
        jSlider1.setMinimum(2);
        jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider1MouseDragged(evt);
            }
        });

        jButton3.setText("QR w GS");
        jButton3.setMaximumSize(new java.awt.Dimension(40, 25));
        jButton3.setMinimumSize(new java.awt.Dimension(40, 25));
        jButton3.setPreferredSize(new java.awt.Dimension(40, 25));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setText("ill conditioned");
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        jButton4.setText("QR w HH");
        jButton4.setMaximumSize(new java.awt.Dimension(40, 25));
        jButton4.setMinimumSize(new java.awt.Dimension(40, 25));
        jButton4.setPreferredSize(new java.awt.Dimension(40, 25));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("QR w GSr");
        jButton5.setMaximumSize(new java.awt.Dimension(40, 25));
        jButton5.setMinimumSize(new java.awt.Dimension(40, 25));
        jButton5.setPreferredSize(new java.awt.Dimension(40, 25));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("new");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel3.setText("jLabel3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jRadioButton3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton6))
                                    .addComponent(jRadioButton1)
                                    .addComponent(jRadioButton5)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(41, 41, 41)
                                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(60, 60, 60))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 83, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(1125, 1125, 1125))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButton3)
                            .addComponent(jButton6))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addGap(554, 554, 554))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(201, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Gauss();
    // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Gausspiv();
    // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        if (jRadioButton1.isSelected()) {
            matrix = 0;
        }
    // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
       if   (jRadioButton2.isSelected()) 
       {matrix=1;
       setmat();
       copymat();}
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        if   (jRadioButton3.isSelected())
        {matrix=2;
        setmat();
        copymat();} 
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
         ndim=jSlider1.getValue();
        jLabel3.setText("dimension "+ndim);
        setmat();
        copymat();// TODO add your handling code here:
    }//GEN-LAST:event_jSlider1MouseDragged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       reort=false;
       doQR();
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        if   (jRadioButton5.isSelected()) 
        {matrix=3;
        setmat();
        copymat();
        }
                // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        
       doQRHH();
       
       
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        reort=true;
        doQR();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if(matrix==2)
        {
        setmat();
        copymat();
        }// TODO add your handling code here:
    }//GEN-LAST:event_jButton6ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

public void setL(){

int i,j;
 for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {

                        L[i][j]=(i==j)? 1 : 0;
                       

                    }
            }
 for (i = 0; i < ndim; i++) {
                for (j = 0; j < ndim; j++) {
                    a[i][j] = a0[i][j];
                }
            }
            for (i = 0; i < ndim; i++) {
                b[i] = b0[i];
            }
}
    
    
    public void setmat () {
// Initiate matrix A and vector b

// well behaved matrix: A[i,i]=N  A[i,j]=1 else

// ill behaved Hilbert matrix : A[i,j]=1/(i+j+1)

 int i, j;
            double s;

            for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {

                       
                        a[i][j]=0;

                    }
            }

        
        {
           

            if (matrix==0) {
                for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {
                        a[i][j] = 1.0 / (double) (i + j + 1);
                     
                    }
                    
                }
            }
            if(matrix==2) {
                for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {
                         a[i][j] =2*( Math.random()-0.5);
                        
                         // if (i == j) {
                         //   a[i][j] = 1.0E-12;
                         // } else {
                         //   a[i][j] =Math.rint( 100000*Math.random());
                           
                        //  } 
                    }
                    

                }
            }


             if(matrix==1) {
                for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {
                        if (i == j) {
                            a[i][j] = ndim;
                        } else {
                            a[i][j] = 1.0;
                            
                        }
                    }
                    

                }
            }

               if(matrix==3) {
                for (i = 0; i < ndim; i++) {
                    for (j = 0; j < ndim; j++) {
                        a[i][j]=0;
                    }
                    

                }
                for (i = 0; i < ndim; i++)
                {a[i][i]=1.0;
                 a[i][ndim-1]=1.0;
                
                for(j=i+1;j<ndim;j++){a[j][i]=-1.0;}
                
                }
                
            }

            for (i = 0; i < ndim; i++) {
                s = 0.0;
                for (j = 0; j < ndim; j++) {
                    s = s + a[i][j] * j;
                }
                b[i] = s;
                //b[i] = ndim - i;
            }

        }
    }

    public void copymat() // make a copy of A and b which will be destroyed
    {
        {
            int i, j;
            for (i = 0; i < ndim; i++) {
                for (j = 0; j < ndim; j++) {
                    a0[i][j] = a[i][j];
                }
            }
            for (i = 0; i < ndim; i++) {
                b0[i] = b[i];
            }
        }
    }

    public void swap(int i1, int i2) {
        int i, j;
        double a1, a2;


        for (i = 0; i < ndim; i++) {
            a1 = a[i1][i];
            a[i1][i] = a[i2][i];
            a[i2][i] = a1;
            a1 = L[i1][i];
            L[i1][i] = L[i2][i];
            L[i2][i] = a1;
        }

        a1 = b[i1];
        b[i1] = b[i2];
        b[i2] = a1;


        i=iindex[i1];
        iindex[i1]=iindex[i2];
        iindex[i2]=i;



    }
      public void printmatQR(int np) {
        int i, j, npp,k,l;  double s,smax;
        npp = np;
        if (npp > ndim) {
            npp = ndim;
        }

         jTextArea1.append(" \n AMAT \n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(a[i][j]) + "  ");
            }

            jTextArea1.append("\n");
        }

        jTextArea1.append(" \n QMAT \n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(L[i][j]) + "  ");
            }
            jTextArea1.append("\n");
        }
       

      jTextArea1.append(" \n RMAT \n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(R[i][j]) + "  ");
            }

            jTextArea1.append("\n");
        }
         jTextArea1.append(" \n Testing orthogonality \n");
         smax=0;
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                
                s=0;
                for(k=0;k<ndim;k++) s=s+L[k][i]*L[k][j];
                
                
                if(i==j)s=s-1.0;
                s=Math.abs(s);
                if(s>smax)smax=s;
            }

            
        }
        jTextArea1.append("maximum deviation : "+smax+"\n"+"\n");
    }
    public void printmat(int np) {
        int i, j, npp;
        npp = np;
        if (npp > ndim) {
            npp = ndim;
        }

        jTextArea1.append(" \n LMAT \n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(L[i][j]) + "  ");
            }
            jTextArea1.append("\n");
        }
        jTextArea1.append(" \n AMAT  | b\n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(a[i][j]) + "  ");
            }
            jTextArea1.append(" | " + df.format(b[i]));
            jTextArea1.append("\n");
        }


    }

    void printmatpiv(int np) {
        int i, j, npp;
        npp = np;
        if (npp > ndim) {
            npp = ndim;
        }

        jTextArea1.append(" \n LMAT \n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(L[i][iindex[j]]) + "  ");
            }
            jTextArea1.append("\n");
        }
        jTextArea1.append(" \n AMAT  | b\n");
        for (i = 0; i < npp; i++) {
            for (j = 0; j < npp; j++) {
                jTextArea1.append(df.format(a[i][j]) + "  ");
            }
            jTextArea1.append(" | " + df.format(b[i]));
            jTextArea1.append("\n");
        }
       
    }

    void LUpiv(boolean verbose) {
// Gauss Elimination
//
// Solution of the SLE  Ax=b
//
// and LU decomposition   A=LU  with pivoting
//
//
        int i, j, k, lmax, l, it;
        double amax,lik;


        for (k = 0; k < ndim; k++) {
            iindex[k] = k;
        }

        for (k = 0; k < ndim - 1; k++) {


            amax = Math.abs(a[k][k]);
            lmax = k;
            for (l = k + 1; l < ndim; l++) {
                if (Math.abs(a[l][k]) > amax) {
                    amax = Math.abs(a[l][k]);
                    lmax = l;
                }
            }

            if(verbose) jTextArea1.append(" step " + k + "  pivot a[" + lmax + "," + k + "] \n");


            if (!(lmax == k)) {
                if(verbose) jTextArea1.append(" exchange" + lmax + "  " + k + " \n");
                swap(lmax, k);
            }


            if (a[k][k] == 0) {
                jTextArea1.append(" matrix singular or pivoting needed! \n");
                throw new RuntimeException();
            }



            for (i = k + 1; i < ndim; i++) {
                lik = a[i][k] / a[k][k];

                b[i]=b[i]-lik*b[k];
                for (j = k; j < ndim; j++) {
                    a[i][j] = a[i][j] - lik * a[k][j];
                    L[i][j] = L[i][j] - lik * L[k][j];

                }
            }
        //printmatpiv(ndim);
        }
    }

    void solvpiv(boolean verbose) {
        int i, j;

        



        x[ndim - 1] = b[ndim - 1] / a[ndim - 1][ndim - 1];
        for (i = ndim - 2; i >= 0; i--) {
            x[i] = b[i];
            for (j = i + 1; j < ndim; j++) {
                x[i] = x[i] - x[j] * a[i][j];
            }
            x[i] = x[i] / a[i][i];
        }
    }

    public void LU(boolean verbose) // LU decomposition
    {
        // Gauss Elimination
//
// Solution of the SLE  Ax=b
//
// and LU decomposition   A=LU  without pivoting
//
//
        int i, j, k;

        for (k = 0; k < ndim - 1; k++) {
            for (i = k + 1; i < ndim; i++) {

                if (a[k][k] == 0) {
                    jTextArea1.append(" matrix singular or pivoting needed! \n");

                    throw new RuntimeException();
                }

                L[i][k] = a[i][k] / a[k][k];
                for (j = k; j < ndim; j++) {
                    a[i][j] = a[i][j] - L[i][k] * a[k][j];
                }
            }

        }
    }

    public void solv(boolean verbose) // Solve the two triangular systems
    {
        int i, j;

        y[0] = b[0];
        for (i = 1; i < ndim; i++) {
            y[i] = b[i];
            for (j = 0; j <= i - 1; j++) {
                y[i] = y[i] - L[i][j] * y[j];
            }
        }


        x[ndim - 1] = y[ndim - 1] / a[ndim - 1][ndim - 1];
        for (i = ndim - 2; i >= 0; i--) {
            x[i] = y[i];
            for (j = i + 1; j < ndim; j++) {
                x[i] = x[i] - x[j] * a[i][j];
            }
            x[i] = x[i] / a[i][i];
        }
    }

    public void printx() {
        int i;

        jTextArea1.append("\n");
        jTextArea1.append(" solution x \n");
        jTextArea1.append(" i          x   \n");
        for (i = 0; i < ndim; i++) {
            jTextArea1.append("" + i + "  " + df2.format(x[i]) + "\n");
        }
         
    }
    
    public void printmaxdev(){
        int i; double dev;
        dev=0.0;
        for(i=0;i<ndim;i++){
        if (Math.abs(x[i]-i)>dev) dev=Math.abs(x[i]-i);}
         jTextArea1.append("max deviation of x" +  "  " + dev + "\n");
    }

    public void check() // print the residual Ax-b
    {
        int i, j;
        double s;


        jTextArea1.append("\n");
        jTextArea1.append(" i      Ax            Ax-b  \n");

        for (i = 0; i < ndim; i++) {
            s = 0.0;
            for (j = 0; j < ndim; j++) {
                s = s + a0[i][j] * x[j];
            }

            jTextArea1.append("" + i + "  " + df.format(s) + "  " + df2.format(b0[i] - s) + "\n");
        }
    }

    public void Gauss() {
        jTextArea1.selectAll();
        jTextArea1.replaceSelection(" Gauss elimination \n");
        setL();
        //copymat();

        jTextArea1.append(" \n  initial values \n");
        printmat(ndim);
        LU(true);

        jTextArea1.append("  \n  after LU decomposition \n");
        printmat(ndim);

        solv(true);

        printx();

        check();
        printmaxdev();

    }

    public void Gausspiv() {
        jTextArea1.selectAll();
        jTextArea1.replaceSelection(" Gauss elimination with partial pivoting\n");
        setL();
        //copymat();

        jTextArea1.append(" \n  initial values \n");
        printmat(ndim);
        LUpiv(true);

        jTextArea1.append("  \n  after LU decomposition \n");
        printmatpiv(ndim);

        solvpiv(true);

        printx();

        check();
        
        printmaxdev();

    }
    
    public void doQR()
    {    jTextArea1.selectAll();
         jTextArea1.replaceSelection(" QR decomposition with Gram-Schmid orthogonalization \n");
        
        setL();
        //copymat();
        
       
        
        QR(true);
        printmatQR(ndim);
        solvQR(true);
        
        printx();
        
        check();

        printmaxdev();
    
    }
    
    
    public void doQRHH()
    {
         jTextArea1.selectAll();
           jTextArea1.replaceSelection(" QR decomposition with Householder reflections \n");
        
       setL();
        //copymat();
        
       
        
        QRHH();
        printmatQR(ndim);
        solvQR(true);
        
        printx();
        
        check();
        
        printmaxdev();

        
    
    }
    
    
    
    
    public void QRHH() {
        int i, j, k,irow,icol;
        double kk, s,z;
        
     
        for(i=0;i<ndim;i++)for(j=0;j<ndim;j++)
        {   R[i][j]=a[i][j];
            if(i==j) L[i][j]=1; else L[i][j]=0; 
        }
        
        
        for (irow = 0; irow < ndim; irow++) {
            //jTextArea1.append("  irow "+irow+"\n");

            s = 0;
            for (i = irow; i < ndim; i++) {
                s = s + R[i][irow] * R[i][irow];
            }
            s = Math.sqrt(s);
            if (R[irow][irow] > 0) {
                kk = -s;
            } else {
                kk = s;
            }
            for (i = irow; i < ndim; i++) {
                uvec[i] = R[i][irow];
            }
            uvec[irow] = uvec[irow] - kk;
            s = 0;
            for (i = irow; i < ndim; i++) {
                s = s + uvec[i] * uvec[i];
            }
            s = Math.sqrt(s);
            for (i = irow; i < ndim; i++) {
                uvec[i] = uvec[i] / s;
            }

             for(icol=irow;icol<ndim;icol++){
                for(i=irow;i<ndim;i++) aicol[i]=R[i][icol]; 
                s = 0;
                for (i = irow; i < ndim; i++) {
                    s = s + aicol[i] * uvec[i];
                }

                for (i = irow; i < ndim; i++) {
                    z = aicol[i] - 2 * s * uvec[i];
                    //jTextArea1.append("  " + z + "\n");
                    R[i][icol] = z;

                }}
              for(icol=0;icol<ndim;icol++){
                for(i=irow;i<ndim;i++) aicol[i]=L[icol][i]; 
                s=0;
                 for (i = irow; i < ndim; i++) {
                    s = s + aicol[i] * uvec[i];
                }

                for (i = irow; i < ndim; i++) {
                    z = aicol[i] - 2 * s * uvec[i];
                    //jTextArea1.append("  " + z + "\n");
                    L[icol][i] = z;

                
            }}
             
             

        }
    
    
    }

    public void QR(boolean verbose) {
        int i, j, k, lmax, l, it;
        double s;
// orthogonalize columns of a
        s = 0.0;
        for (i = 0; i < ndim; i++) {
            s = s + a[i][0] * a[i][0];
        }
        s = Math.sqrt(s);
        R[0][0] = s;
        for (i = 0; i < ndim; i++) {
            L[i][0] = a[i][0] / s;
        }
        for (k = 1; k < ndim; k++) // new column k
        {
            for (i = 0; i < ndim; i++) {
                bk[i] = a[i][k];
            }

           

            for (i = 0; i < k; i++) // orthogonalize to  qi 
            {
                s = 0.0;
                for (j = 0; j < ndim; j++) {
                    s = s + a[j][k] * L[j][i];
                }
                R[i][k] = s;
              
                for (j = 0; j < ndim; j++) {
                    bk[j] = bk[j] - s * L[j][i];
                }
            }
           
            
            if(reort){
             for (i = 0; i < k; i++) // orthogonalize to  qi once more
            {
                s = 0.0;
                for (j = 0; j < ndim; j++) {
                    s = s + bk[j] * L[j][i];
                }
                R[i][k] = R[i][k]+s;
                
                for (j = 0; j < ndim; j++) {
                    bk[j] = bk[j] - s * L[j][i];
                }
            }
            }
           
            
            s = 0.0;
            for (i = 0; i < ndim; i++) {
                s = s + bk[i] * bk[i];
            }
            s = Math.sqrt(s);
            
            R[k][k] = s;
            for (i = 0; i < ndim; i++) {
                L[i][k] = bk[i] / s;
            }
           

        }
    }

void solvQR(boolean verbose)
{ int i,j; double s;

 for(i=0;i<ndim;i++)
 {
     s=0.0;
     for(j=0;j<ndim;j++) s=s+L[j][i]*b[j];
     qb[i]=s;
 }

 if(verbose)
 {
     jTextArea1.append(" transformed rhs vector \n");
 for(i=0;i<ndim;i++) jTextArea1.append(""+df.format(qb[i]));
 jTextArea1.append("\n");
 }
 x[ndim-1]=qb[ndim-1]/R[ndim-1][ndim-1];

 for(i=ndim-2;i>=0;i--)
 { s=qb[i];
 for(j=i+1;j<ndim;j++) s=s-x[j]*R[i][j];
 s=s/R[i][i];
 x[i]=s;
 }

}


    public class myPanel extends javax.swing.JPanel {

        public void paintComponent(Graphics g) {


            super.paintComponent(g);

        }
    }
}
