public static LSFStrings Calculate(LSFInput input)
        {
            var ind = input.XValues;
            var de = input.YValues;
            var uncertain = input.YUncertainties;

            if (ind.Count != de.Count)
                throw new Exception("there are a different number of independent and dependent values");

            if (ind.Count != uncertain.Count)
                throw new Exception("there are a different number of values and uncertanties");

            //calculate relevant LSF constants
            int n = ind.Count;
            var indSum = Summation(ind);
            var deSum = Summation(de);
            var uncertSumPow = SummationPowered(uncertain);
            var indSumPow = SummationPowered(ind);
            var crossSum = SummationMulti(ind, de);

            var lsfResult = CalculateLeastSquaresFit(n, indSum, deSum, indSumPow, crossSum, uncertSumPow);

            #region format everything

            var retStruct = new LSFStrings();
            retStruct.IndSum = "∑x=";
            retStruct.DeSum = "∑y=";
            retStruct.CrossSum = "∑xy=";
            retStruct.IndPowerSum = "∑x^2=";
            retStruct.UncertPowerSum = "∑δy^2=";

            foreach (var d in ind){
                retStruct.IndSum += (d + "+");
            }
            retStruct.IndSum = retStruct.IndSum.Remove(retStruct.IndSum.Count() - 1);

            foreach (var d in de){
                retStruct.DeSum += (d + "+");
            }
            retStruct.DeSum = retStruct.DeSum.Remove(retStruct.DeSum.Count() - 1);

            for (int i = 0; i < n; i++){
                retStruct.CrossSum += ind[i].ToString() + "*" + de[i].ToString() + "+";
            }
            retStruct.CrossSum = retStruct.CrossSum.Remove(retStruct.CrossSum.Count() - 1);

            foreach (var d in ind){
                retStruct.IndPowerSum += (d + "^2+");
            }
            retStruct.IndPowerSum = retStruct.IndPowerSum.Remove(retStruct.IndPowerSum.Count() - 1);

            foreach (var d in uncertain){
                retStruct.UncertPowerSum += (d + "^2+");
            }
            retStruct.UncertPowerSum = retStruct.UncertPowerSum.Remove(retStruct.UncertPowerSum.Count() - 1);

            //now for the second part
            retStruct.SlopeEquation = "m={" + n + "*" + crossSum + "-" + indSum + "*" + deSum + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}";
            retStruct.InterceptEquation = "b={" + indSum + "^2*" + deSum + "-" + indSum + "*" + crossSum + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}";
            retStruct.YUncertEquation = "s_y=sqrt{{" + uncertSumPow + "} over {" + n + "-2}}";
            retStruct.InterceptUncertEquation = "s_b=" + lsfResult.Sy + "*sqrt{{" + indSumPow + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}}";
            retStruct.SlopeUncertEquation = "s_m=" + lsfResult.Sy + "*sqrt{{" + n + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}}";

            //add the constants generated to the end of the equations in question
            retStruct.IndSum += "=" + indSum;
            retStruct.IndPowerSum += "=" + indSumPow;
            retStruct.DeSum += "=" + deSum;
            retStruct.CrossSum += "=" + crossSum;
            retStruct.InterceptEquation += "=" + lsfResult.YIntercept;
            retStruct.InterceptUncertEquation += "=" + lsfResult.Sb;
            retStruct.SlopeEquation += "=" + lsfResult.Slope;
            retStruct.SlopeUncertEquation += "=" + lsfResult.Sm;
            retStruct.UncertPowerSum += "=" + uncertSumPow;
            retStruct.YUncertEquation += "=" + lsfResult.Sy;
            retStruct.NumSamples = n;

            #endregion

            return retStruct;
        }
예제 #2
0
        public static LSFStrings Calculate(LSFInput input)
        {
            var ind       = input.XValues;
            var de        = input.YValues;
            var uncertain = input.YUncertainties;

            if (ind.Count != de.Count)
            {
                throw new Exception("there are a different number of independent and dependent values");
            }

            if (ind.Count != uncertain.Count)
            {
                throw new Exception("there are a different number of values and uncertanties");
            }

            //calculate relevant LSF constants
            int n            = ind.Count;
            var indSum       = Summation(ind);
            var deSum        = Summation(de);
            var uncertSumPow = SummationPowered(uncertain);
            var indSumPow    = SummationPowered(ind);
            var crossSum     = SummationMulti(ind, de);

            var lsfResult = CalculateLeastSquaresFit(n, indSum, deSum, indSumPow, crossSum, uncertSumPow);

            #region format everything

            var retStruct = new LSFStrings();
            retStruct.IndSum         = "∑x=";
            retStruct.DeSum          = "∑y=";
            retStruct.CrossSum       = "∑xy=";
            retStruct.IndPowerSum    = "∑x^2=";
            retStruct.UncertPowerSum = "∑δy^2=";

            foreach (var d in ind)
            {
                retStruct.IndSum += (d + "+");
            }
            retStruct.IndSum = retStruct.IndSum.Remove(retStruct.IndSum.Count() - 1);

            foreach (var d in de)
            {
                retStruct.DeSum += (d + "+");
            }
            retStruct.DeSum = retStruct.DeSum.Remove(retStruct.DeSum.Count() - 1);

            for (int i = 0; i < n; i++)
            {
                retStruct.CrossSum += ind[i].ToString() + "*" + de[i].ToString() + "+";
            }
            retStruct.CrossSum = retStruct.CrossSum.Remove(retStruct.CrossSum.Count() - 1);

            foreach (var d in ind)
            {
                retStruct.IndPowerSum += (d + "^2+");
            }
            retStruct.IndPowerSum = retStruct.IndPowerSum.Remove(retStruct.IndPowerSum.Count() - 1);

            foreach (var d in uncertain)
            {
                retStruct.UncertPowerSum += (d + "^2+");
            }
            retStruct.UncertPowerSum = retStruct.UncertPowerSum.Remove(retStruct.UncertPowerSum.Count() - 1);

            //now for the second part
            retStruct.SlopeEquation           = "m={" + n + "*" + crossSum + "-" + indSum + "*" + deSum + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}";
            retStruct.InterceptEquation       = "b={" + indSum + "^2*" + deSum + "-" + indSum + "*" + crossSum + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}";
            retStruct.YUncertEquation         = "s_y=sqrt{{" + uncertSumPow + "} over {" + n + "-2}}";
            retStruct.InterceptUncertEquation = "s_b=" + lsfResult.Sy + "*sqrt{{" + indSumPow + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}}";
            retStruct.SlopeUncertEquation     = "s_m=" + lsfResult.Sy + "*sqrt{{" + n + "} over {" + n + "*" + indSumPow + "-" + indSum + "^2}}";

            //add the constants generated to the end of the equations in question
            retStruct.IndSum                  += "=" + indSum;
            retStruct.IndPowerSum             += "=" + indSumPow;
            retStruct.DeSum                   += "=" + deSum;
            retStruct.CrossSum                += "=" + crossSum;
            retStruct.InterceptEquation       += "=" + lsfResult.YIntercept;
            retStruct.InterceptUncertEquation += "=" + lsfResult.Sb;
            retStruct.SlopeEquation           += "=" + lsfResult.Slope;
            retStruct.SlopeUncertEquation     += "=" + lsfResult.Sm;
            retStruct.UncertPowerSum          += "=" + uncertSumPow;
            retStruct.YUncertEquation         += "=" + lsfResult.Sy;
            retStruct.NumSamples               = n;

            #endregion

            return(retStruct);
        }