/*
 * -------------------------------------------------------------------------
 *    $Id: Roots.java,v 1.5 2004/09/01 18:07:54 estewart Exp $
 * -------------------------------------------------------------------------
 *    Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *    This software is confidential information which is proprietary to
 *    and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *    disclosure is subject to the terms of an appropriate license
 *    agreement.
 *
 *    VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *    SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *    BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *    NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *    AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *    ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.javagrande;
import com.imsl.chart.*;
import com.imsl.math.*;
import java.awt.*;
import java.applet.Applet;


public class Roots extends Applet
{
    private Chart    chart = null;


    private Complex[] testRootsOfUnity(int ndeg)
        throws ZeroPolynomial.DidNotConvergeException
    {
        double coef[] = new double[ndeg+1];
        coef[0] = -1.0;
        coef[ndeg/2] = 2;
        coef[ndeg/2+1] = -3;
        coef[ndeg] = 1.0;
        double radius[] = new double[ndeg];
        boolean status[] = new boolean[ndeg];

        ZeroPolynomial zp = new ZeroPolynomial();
        return zp.computeRoots(coef);
    }
    
    
    public void init()
    {
        Complex roots[];
        try {
            roots = testRootsOfUnity(50);
        } catch (ZeroPolynomial.DidNotConvergeException e) {
            return;
        }

        chart = new Chart(this);
        chart.getChartTitle().setTitle(new Text("Roots of a Polynomial"));
        AxisXY axis = new AxisXY(chart);
        //axis.setCross(0.0, 0.0);
        axis.getAxisX().getAxisTitle().setTitle("real");
        axis.getAxisY().getAxisTitle().setTitle("imaginary");

        int n = roots.length;
        double x[] = new double[n];
        double y[] = new double[n];
        for (int k = 0;  k < x.length;  k++) {
            x[k] = roots[k].real();
            y[k] = roots[k].imag();
        }

        Data data = new Data(axis, x, y);
        data.setDataType(Data.DATA_TYPE_MARKER);
        data.setMarkerType(Data.MARKER_TYPE_HOLLOW_SQUARE);
        data.setMarkerColor(Color.blue);
    }

    public void update(Graphics g)
    {
        chart.update(g);
    }
    
    public void paint(Graphics g)
    {
        chart.paint(g);
    }


    public static void main(String argv[])
    {
        Roots ex = null;

        Frame frame = new Frame();
        ex = new Roots();
        frame.add(ex);
        ex.init();
        frame.setSize(new Dimension(400,400));
        frame.show();
    }

}