text:
//Built using buildjs.py script - do not hand edit

// econgraphs.js
var econgraphs = {functions: {}};

// budget/budget.js
econgraphs.functions.budget = {

    addBudgetMethods: function (b, params) {

        b.prices = b.prices || [];

        b.income = b.income || 0;

        b.bundle = b.bundle || [];

        b.setPrice = function(price, index) {
            b.prices[index] = price;
        };

        b.setPrices = function(prices) {
            b.prices = prices || b.prices;
            return b;
        };

        b.setIncome = function(income) {
            b.income = income || b.income;
            return b;
        };

        b.update = function(params) {
            if(params) {
                b.setPrices(params.prices);
                b.setIncome(params.income);
            }
        };

        // For now, b is a simple multiplication; could be adjusted for volume discounts, etc.
        b.expenditure = function(quantity,price) {
            return quantity * price;
        };

        b.cost = function(bundle,prices) {

            // used store prices (and potentially bundle) if none passed in
            bundle = bundle || b.bundle;
            prices = prices || b.prices;

            var totalCost = 0;

            for(var good=0; good < bundle.length; good++) {
                totalCost += b.expenditure(bundle[good], prices[good])
            }

            return totalCost;
        };

        b.isAffordable = function(bundle,prices,income) {

            // Use stored income if none passed in
            income = income || b.income;

            return (b.cost(bundle, prices) <= income)
        };

        b.area = function(xDomain,yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            var areaPoints = [{x: xDomain.min, y:yDomain.min}];

            var constraintPoints = b.points(xDomain,yDomain).sort(sortObjects('x'));

            if(constraintPoints[0].y == yDomain.max) {
                areaPoints.push({x:xDomain.min, y:yDomain.max});
            }

            if (constraintPoints[1].x == xDomain.max) {
                constraintPoints.push({x: xDomain.max, y: yDomain.min});
            }

            return d3.merge([areaPoints,constraintPoints]);

        };

        b.update(params);

        return b;

    }

};

// budget/simple.js
/**
 * Created by cmakler on 10/30/14.
 */

econgraphs.functions.budget.SimpleBudgetConstraint = function () {

    return function (params) {

        // create linear function I - PxX - PyY; this must be positive in order for the constraint to be met.
        var b = new kg.functions.Linear({definitionType: 'standard'});

        b.updateParams = function(params) {

            if(typeof params == 'function') {
                params = params();
            }

            params = params || {income: 0, prices: [0,0]};

            var px = parseFloat(params.px || params.prices[0]),
                py = parseFloat(params.py || params.prices[1]),
                income = parseFloat(params.income);

            b.setCoefficients({a: -px, b: -py, c: income});

            return b;

        };

        b.updateParams(params);

        return econgraphs.functions.budget.addBudgetMethods(b, params);
    }

}();

// utility/utility.js
econgraphs.functions.utility = {

    addUtilityMethods: function (u, params) {

        // If utility parameter is included, either as a number or a point,
        // set the level of the function to that utility value (or the utility value of the specified point)
        if (params && params.utility) {
            u.setLevel(params.utility);
        }

        // Functions that can be called for any utility function

        u.setBundle = function (bundle) {
            u.bases = bundle || [];
            return u;
        };

        u.setBudgetConstraint = function(budgetConstraintParams) {
            if(budgetConstraintParams) {
                if (!u.budgetConstraint){
                    u.budgetConstraint = new econgraphs.functions.budget.SimpleBudgetConstraint;
                }
                u.budgetConstraint.update(budgetConstraintParams);
            }
        };

        u.mu = function (bundle, index) {
            bundle = bundle || u.bases;
            return u.derivative(index).value(bundle);
        };

        u.mux = function (bundle) {
            bundle = bundle || u.bases;
            return u.mu(bundle, 1);
        };

        u.muy = function (bundle) {
            bundle = bundle || u.bases;
            return u.mu(bundle, 2);
        };

        u.mrs = function (bundle) {
            bundle = bundle || u.bases;
            if (u.muy(bundle) == 0 || u.mux(bundle) == 'undefined') {
                return 'undefined'
            } else {
                return u.mux(bundle) / u.muy(bundle)
            }
        };

        u.mrsTangentLine = function(bundle) {
            var point = {x: bundle[0], y:bundle[1]},
                slope = -u.mrs(bundle),
                l = new kg.functions.Linear({definitionType:'point-slope', point:point, slope: slope});
            return l;
        };

        u.utility = function(bundle) {
            bundle = bundle || u.bases;
            return u.value([bundle.x,bundle.y])
        };

        u.indirectUtility = function (income,px,py) {

            return u.value(u.optimalBundle(income, px, py));

        };

        // Given two bundles, evaluates whether agent prefers first or second, or is indifferent
        u.bundlePreferred = function(bundles, t) {

            var u1 = u.utility(bundles[0]),
                u2 = u.utility(bundles[1]),
                percentUilityDifference = (u2 - u1)/(0.5*(u1 + u2)),
                tolerance = t || 0.01; // percent difference within which one is thought to be indifferent

            if(percentUilityDifference > tolerance) {
                return 2; //second bundle preferred
            }

            if(percentUilityDifference < -tolerance) {
                return 1; //first bundle preferred
            }

            return 0; //indifferent between two bundles

        };

        // Find the optimal bundle for a given income and prices
        u.optimalBundle = function (income, px, py) {

            if (u.budgetConstraint) {
                income = income || u.budgetConstraint.income;
                px = px || u.budgetConstraint.prices[0];
                py = py || u.budgetConstraint.prices[1];
            }

            return u._optimalBundle(income,px,py);
        };

        // Find the indifferece curve passing through the optimal bundle
        u.optimalIndifferenceCurve = function (income, px, py) {

            return u.setLevel(u.optimalBundle(income,px,py))

        };

        /*

        Find the price-consumption curve for a given income and other price

        The pccParams object should have the following structure:
        {
            good: the good whose price we are going to vary; must be 'x' or 'y'; 'x' by default
            minPrice: the minimum price to evaluate (0 by default)
            maxPrice: the maximum price to evaluate (50 by default)
            income: the consumer's income, OR a bundle {x:x, y:y} to be evaluated at current prices
            otherPrice: the price of the other good
        }

        */

        u.priceConsumptionCurve = function (pccParams) {

            return {

                points: function (xDomain, yDomain) {

                    var px,
                        py,
                        isGoodX = ('y' != pccParams['good']),
                        minPrice = pccParams['minPrice'] || 0,
                        maxPrice = pccParams['maxPrice'] || 100,
                        income = pccParams['income'],
                        endowment = pccParams['endowment']||{},
                        samplePoints = pccParams['samplePoints'] || 51,
                        otherPrice = pccParams['otherPrice'],
                        priceConsumptionFunction = function (price) {
                            px = isGoodX ? price : otherPrice;
                            py = isGoodX ? otherPrice : price;
                            if (endowment.hasOwnProperty('x')) {
                                income = endowment.x * px + endowment.y * py;
                            }
                            return u.optimalBundle(income, px, py);
                        };

                    return functionPoints(priceConsumptionFunction, xDomain, yDomain, {min: minPrice, max: maxPrice, dependentVariable: 'p'});

                }
            }
        };

        /*

         Find the income expansion path for a given set of prices.
         The incomeExpansionParams object should have the following structure:

             {
                minIncome: the minimum income to evaluate (0 by default)
                maxIncome: the maximum income to evaluate (50 by default)
                px: price of x
                py: price of y
             }

         */

        u.incomeConsumptionCurve = function (iccParams) {

            return {

                points: function (xDomain, yDomain) {

                    var minIncome = iccParams['minIncome'] || 0,
                        maxIncome = iccParams['maxIncome'] || 50,
                        px = iccParams['px'],
                        py = iccParams['py'],
                        samplePoints = iccParams['samplePoints'] || 51,
                        incomeConsumptionFunction = function (income) {
                            return u.optimalBundle(income, px, py);
                        };

                    return functionPoints(incomeConsumptionFunction, xDomain, yDomain, {min: minIncome, max: maxIncome, dependentVariable: 'i'});

                }
            }
        };

        /*

        Find the Engel curve for a given set of prices
        The engelCurveParams object should have the following structure:
        {
            good: the good whose quantity demanded we are going to plot
            minIncome: the minimum income to evaluate (0 by default)
            maxIncome: the maximum income to evaluate (50 by default)
            px: price of x
            py: price of y
        }

        */

        u.engelCurve = function(engelParams) {

            return {

                points: function (xDomain, yDomain) {

                    var isGoodX = ('y' != engelParams['good']),
                        px = engelParams['px'],
                        py = engelParams['py'],
                        engelFunction = function (income) {
                            return isGoodX ? u.optimalBundle(income, px, py)[0] : u.optimalBundle(income, px, py)[1];
                        };

                    return functionPoints(engelFunction, xDomain, yDomain, {dependentVariable: 'y'});
                }
            }

        };

        /*

         Find the demand curve for a given income and other price

         The demandParams object should have the following structure:
             {
                 good: the good whose price we are going to vary; must be 'x' or 'y'; 'x' by default
                 minPrice: the minimum price to evaluate (0 by default)
                 maxPrice: the maximum price to evaluate (50 by default)
                 income: the consumer's income
                 otherPrice: the price of the other good
             }

         */
        u.demandCurve = function(demandParams) {

            return {

                points: function (xDomain, yDomain) {

                    yDomain = domainAsObject(yDomain);

                    var compensatedIncome,
                        isGoodX = ('y' != demandParams['good']),
                        compensationPrice = demandParams['compensationPrice'] || 0,
                        income = demandParams['income'],
                        numberOfConsumers = demandParams['numberOfConsumers'] || 1,
                        minPrice = demandParams['minPrice'] || yDomain.min,
                        maxPrice = demandParams['maxPrice'] || yDomain.max,
                        otherPrice = demandParams['otherPrice'],
                        samplePoints = demandParams['samplePoints'] || 51,
                        demandFunction = function(price) {
                            if (isGoodX) {
                                compensatedIncome = (compensationPrice > 0) ? u.compensatedIncome(income, compensationPrice, price, otherPrice) : income;
                                return u.optimalBundle(compensatedIncome, price, otherPrice)[0] * numberOfConsumers;
                            } else {
                                return u.optimalBundle(income, otherPrice, price)[1] * numberOfConsumers;
                            }
                        };

                    return functionPoints(demandFunction,xDomain,yDomain,{dependentVariable:'y', min:minPrice, max:maxPrice});
                },

                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var points = this.points(xDomain,yDomain),
                        minPrice = demandParams['minPrice'] || yDomain.min,
                        maxPrice = demandParams['maxPrice'] || yDomain.max;

                    points.push({x: 0, y: maxPrice});
                    points.push({x: 0, y: minPrice});

                    return points;

                }
            }
        };

        // Find the lowest possible cost for a given level of utility, given px and py
        u.lowestPossibleCost = function (utility, px, py) {

            if (u.budgetConstraint) {
                px = px || u.budgetConstraint.prices[0];
                py = py || u.budgetConstraint.prices[1];
            }

            return u._lowestPossibleCost(utility, px, py);
        };

        // Return the bundle that provides a given level of utility at lowest cost
        u.lowestCostBundle = function (utility,px,py) {

            // set income to lowest necessary to achieve utility
            var income = u.lowestPossibleCost(utility,px,py);
            return u.optimalBundle(income,px,py);

        };

        // Return the income necessary to achieve v(income,px1,py) if px is now px2
        u.compensatedIncome = function(income,px1,px2,py) {
            var utility = u.value(u.optimalBundle(income, px1, py));
            return u._lowestPossibleCost(utility, px2, py);

        };

        // Return the decomposition bundle for a price change from px1 to px2
        u.decompositionBundle = function (income, px1, px2, py) {

            return u.optimalBundle(u.compensatedIncome(income,px1,px2,py), px2, py);

        };

        return u;

    }

};

// utility/cobbdouglas.js
/**
 * Created by cmakler on 10/27/14.
 */

econgraphs.functions.utility.CobbDouglas = function () {

    return function (params) {

        var alpha, beta, returnsToScale, xProportion, yProportion;

        // Create a monomial of the form u(x,y) = (x^alpha)(y^beta)
        var u = new kg.functions.Monomial({coefficient: 1, powers: [alpha, beta]});

        // Add utility function methods (marginal utility, MRS, etc.)
        u = econgraphs.functions.utility.addUtilityMethods(u, params);

        u.updateParams = function(params) {

            if (typeof params == 'function') {
                params = params();
            }

            // Alpha is the power to which the first (x) variable is raised. Set to 0.5 by default.
            u.alpha = (params && params.hasOwnProperty('alpha')) ? params['alpha'] : 0.5;

            // Beta is the power to which the second (y) variable is raised. Set to 1 - alpha by default.
            u.beta = (params && params.hasOwnProperty('beta')) ? params['beta'] : 1 - u.alpha;

            // A Cobb-Douglas function is constant returns to scale if alpha + beta = 1,
            // diminishing returns to scale if alpha + beta < 1, and increasing returns to scale if alpha + beta > 1.
            returnsToScale = u.alpha + u.beta;

            // A nice feature of Cobb-Douglas functions is that their optimal bundles
            // are characterized by proportional spending on X and Y.
            xProportion = u.alpha / (u.alpha + u.beta),
            yProportion = u.beta / (u.alpha + u.beta);

            u.setPowers([u.alpha, u.beta]);

            return u;

        };

        u.updateParams(params);

        // Find the optimal bundle for a given income and prices
        u._optimalBundle = function (income, px, py) {

            return [xProportion * income / px, yProportion * income / py]
        };

        // Find the lowest possible cost for a given level of utility, given px and py
        u._lowestPossibleCost = function (utility, px, py) {
            return returnsToScale*Math.pow(utility,1/returnsToScale)*Math.pow(px/ u.alpha,xProportion)*Math.pow(py/ u.beta,yProportion);
        };

        u.preferred = {area: function(xDomain,yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            if(u.alpha == 0) {
                return [
                    {x: xDomain.min, y: u.yValue(xDomain.min)},
                    {x: xDomain.min, y: yDomain.max},
                    {x: xDomain.max, y: yDomain.max},
                    {x: xDomain.max, y: u.yValue(xDomain.max)}
                ]
            }

            if(u.beta == 0) {
                return [
                    {x: u.xValue(yDomain.min), y: yDomain.min},
                    {x: xDomain.max, y: yDomain.min},
                    {x: xDomain.max, y: yDomain.max},
                    {x: u.xValue(yDomain.max), y: yDomain.max}
                ]
            }

            var allPoints = d3.merge([u.points(xDomain, yDomain), u.points(xDomain, yDomain, true)]).sort(sortObjects('x'));

            allPoints.push({x: xDomain.max, y: yDomain.max});

            return allPoints;

        }};

        u.dispreferred = {area: function (xDomain, yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            /* This doesn't work yet
            if (u.alpha == 0) {
                return [
                    {x: xDomain.min, y: u.yValue(xDomain.min)},
                    {x: xDomain.min, y: yDomain.max},
                    {x: xDomain.max, y: yDomain.max},
                    {x: xDomain.max, y: u.yValue(xDomain.max)}
                ]
            }

            if (u.beta == 0) {
                return [
                    {x: u.xValue(yDomain.min), y: yDomain.min},
                    {x: xDomain.max, y: yDomain.min},
                    {x: xDomain.max, y: yDomain.max},
                    {x: u.xValue(yDomain.max), y: yDomain.max}
                ]
            }
            */

            var allPoints = d3.merge([u.points(xDomain, yDomain), u.points(xDomain, yDomain, true)]).sort(sortObjects('x'));

            allPoints.push({x: xDomain.max, y: yDomain.min});
            allPoints.push({x: xDomain.min, y: yDomain.min});
            allPoints.push({x: xDomain.min, y: yDomain.max});

            return allPoints;

        }};

        u.mrsOfX = function(bundle) {

            return {points: function (xDomain, yDomain) {

                var points = [];

                var x, y;

                for (var i = 0; i < 51; i++) {

                    x = xDomain.min + (i / 50) * (xDomain.max - xDomain.min);
                    y = u.mrs(x);
                    if (inRange(y, yDomain)) {
                        points.push({x: x, y: y});
                    }
                }
            }
            }

        };

        return u;
    }

}();

// utility/complements.js
/**
 * Created by cmakler on 10/27/14.
 */

// This creates a utility function of the form u(x,y) = min(ax,by)
econgraphs.functions.utility.PerfectComplements = (function () {

    return function (params) {

        var xFunction = new kg.functions.Linear({definitionType: 'standard', a: 1, b: 0, c: 0}),
            yFunction = new kg.functions.Linear({definitionType: 'standard', a: 0, b: 1, c: 0});

        var u = new kg.functions.Min({minimands: [xFunction,yFunction]});

        function ensureValidNumbers() {
            u.level = u.level || 0;
            u.a = u.a || 1;
            u.b = u.b || 1;
        }

        u.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            if (params && params.hasOwnProperty('a')) {
                u.a = params.a;
                u.minimands[0].setCoefficient(1, u.a)
            }

            if (params && params.hasOwnProperty('a')) {
                u.b = params.b;
                u.minimands[1].setCoefficient(2, u.b)
            }
            return u;

        };

        u.updateParams(params);

        // Find the optimal bundle for a given budget constraint
        u._optimalBundle = function (income, px, py) {

            // The highest affordable utility comes when ax = by = highest utility
            var highestUtility = income / ((px/ u.a) + (py / u.b));

            return [highestUtility / u.a, highestUtility / u.b ];
        };

        // Find the lowest possible cost for a given level of utility, given px and py
        u._lowestPossibleCost = function (utility, px, py) {
            return utility * ((px / u.a) + (py / u.b));
        };

        u.xValue = function (y) {
            ensureValidNumbers();
            if (y > u.level / u.b) {
                return u.level / u.a;
            } else {
                return 'undefined';
            }
        };

        u.yValue = function(x) {
            ensureValidNumbers();
            if(x > u.level/ u.a) {
                return u.level / u.b;
            } else {
                return 'undefined';
            }
        };

        u.points = function(xDomain,yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            ensureValidNumbers();
            var critical_x = u.level / u.a,
                critical_y = u.level / u.b;
            if(inRange(critical_x,xDomain) && inRange(critical_y,yDomain)) {
                return [
                    {x: critical_x, y: yDomain.max},
                    {x: critical_x, y: critical_y},
                    {x: xDomain.max, y: critical_y}
                ];
            } else {
                return null;
            }
        };

        u.preferred = {area: function (xDomain, yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            var points = u.points(xDomain, yDomain);

            points.push({x: xDomain.max, y: yDomain.max});

            return points;

        }};

        return econgraphs.functions.utility.addUtilityMethods(u,params);

    }

}());

// utility/substitutes.js
/**
 * Created by cmakler on 10/27/14.
 */

econgraphs.functions.utility.PerfectSubstitutes = (function () {

    return function (params) {

        var u = new kg.functions.Linear({definitionType: 'standard'});

        u = econgraphs.functions.utility.addUtilityMethods(u, params);

        u.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            if(params) {
                u.setCoefficients({a: params.a, b: params.b, c: 0});
            }

            return u;

        };

        u.preferred = {area: function (xDomain, yDomain) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            var allPoints = u.points(xDomain, yDomain);

            // TODO: this fails when horizontal
            allPoints.push({x: xDomain.max, y: yDomain.min});
            allPoints.push({x: xDomain.max, y: yDomain.max});
            allPoints.push({x: xDomain.min, y: yDomain.max});

            return allPoints;

        }};

        // Find the optimal bundle for a given budget constraint
        u._optimalBundle = function (income, px, py) {

            var onlyX = [income / px, 0],
                onlyY = [0, income / py],
                mix = [0.5 * income / px, 0.5 * income / py];

            if(u.value(onlyX) > u.value(onlyY)) {
                return onlyX;
            } else if (u.value(onlyY) > u.value(onlyX)) {
                return onlyY;
            } else {
                return mix;
            }

        };

        // Find the lowest possible cost for a given level of utility, given px and py
        // In this case, to obtain U units of utility, this can be done with U/a units of X or U/b units of Y.
        u._lowestPossibleCost = function (utility, px, py) {
            return Math.min(px * utility / u.a(), py * utility / u.b());
        };

        return u;

    }

}());

// utility/ces.js
/**
 * Created by cmakler on 1/26/15.
 */

econgraphs.functions.utility.CES = function () {

    return function (params) {

        var u = new kg.functions.Generic;

        u.alpha = 0.5;
        u.r = 0.5;

        u.value = function(bundle) {
            return u.alpha*Math.pow(bundle[0], u.r) + (1- u.alpha)*Math.pow(bundle[1], u.r)
        };

        u.xValue = function(y) {

            var num = u.level - (1- u.alpha)*Math.pow(y, u.r),
                dem = u.alpha;

            return Math.pow(num/dem,1/ u.r);

        };

        u.yValue = function (x) {

            return Math.pow((u.level - u.alpha * Math.pow(x, u.r)) / (1 - u.alpha), 1 / u.r)

        };

        u.points = function(xDomain,yDomain,yIsIndependent) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            var points = [];

            var x, y;

            for (var i = 0.01; i < 51; i++) {

                if (yIsIndependent) {
                    // Sample 51 points along the Y domain
                    y = yDomain.min + (i / 50) * (yDomain.max - yDomain.min);
                    x = u.xValue(y);
                    if (inRange(x, xDomain)) {
                        points.push({x: x, y: y});
                    }
                } else {
                    // Sample 51 points along the X domain
                    x = xDomain.min + (i / 50) * (xDomain.max - xDomain.min);
                    y = u.yValue(x);
                    if (inRange(y, yDomain)) {
                        points.push({x: x, y: y});
                    }
                }
            }

            return points;
        }

        // Add utility function methods (marginal utility, MRS, etc.)
        u = econgraphs.functions.utility.addUtilityMethods(u);

        u.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            if (params) {

                u.alpha = params['alpha'] || u.alpha;
                u.r = params['r'] - 0.01 || u.r;

                if(params.hasOwnProperty('s')) {
                    if(params.s >= 0) {
                        u.r = params.s - 0.01; // Cheat so that 1 = 0.999
                    } else {
                        u.r = params.s / (1.01 + params.s) // Cheat so that -1 => -1 / 0.001
                    }
                }

                if (u.r == 0) {
                    u.r = 0.01;
                }

            }

            return u;

        };

        u.updateParams(params);

        // Find the optimal bundle for a given income and prices
        u._optimalBundle = function (income, px, py) {

            var s = 1 / (1 - u.r),
                denominator = Math.pow(u.alpha,s)*Math.pow(px, 1-s) + Math.pow(1-u.alpha,s)*Math.pow(py, 1-s),
                optimal_x = income * Math.pow(px/ u.alpha,-s)/denominator,
                optimal_y = income * Math.pow(py/ (1 - u.alpha),-s) / denominator;

            return [optimal_x,optimal_y];
        };

        // Find the lowest possible cost for a given level of utility, given px and py
        u._lowestPossibleCost = function (utility, px, py) {

            var s = 1 / (1 - u.r),
                denominator = Math.pow(u.alpha, s) * Math.pow(px, 1 - s) + Math.pow(1 - u.alpha, s) * Math.pow(py, 1 - s),
                x_coefficient = Math.pow(px / u.alpha, -s) / denominator,
                y_coefficient = Math.pow(py / (1 - u.alpha), -s) / denominator,
                scale_factor = u.alpha*Math.pow(x_coefficient, u.r) + (1- u.alpha)*Math.pow(y_coefficient, u.r),

                c = Math.pow(utility/scale_factor, 1/ u.r);

                return c;
        };

        return u;
    }

}();

// utility/inferior.js
/**
 * Created by cmakler on 1/26/15.
 *
 * Note: this implements the utility function described in http://www.hindawi.com/journals/isrn/2012/608645/
 */

econgraphs.functions.utility.Inferior = function () {

    return function (params) {

        var u = new kg.functions.Generic;

        u.xmin = 10;
        u.ymax = 40;
        u.alpha = 1;
        u.delta = 1;

        u.valueOfX = function(x) {return u.alpha * Math.log(x - u.xmin)};
        u.valueOfY = function(y) {return -(u.alpha + u.delta) * Math.log(u.ymax - y)};

        u.value = function (bundle) {
            return u.valueOfX(bundle[0]) + u.valueOfY(bundle[1]);
        };

        u.xValue = function (y) {

            return u.xmin + Math.exp((u.level - u.valueOfY(y))/ u.alpha);

        };

        u.yValue = function (x) {

            return u.ymax + Math.exp((u.valueOfX(x) - u.level)/(u.alpha + u.delta));

        };

        u.points = function (xDomain, yDomain, yIsIndependent) {

            xDomain = domainAsObject(xDomain);
            yDomain = domainAsObject(yDomain);

            xDomain.min = u.xmin;
            yDomain.max = u.ymax;

            var points = [];

            var x, y;

            for (var i = 0.01; i < 51; i++) {

                if (yIsIndependent) {
                    // Sample 51 points along the Y domain
                    y = yDomain.min + (i / 50) * (yDomain.max - yDomain.min);
                    x = u.xValue(y);
                    if (inRange(x, xDomain)) {
                        points.push({x: x, y: y});
                    }
                } else {
                    // Sample 51 points along the X domain
                    x = xDomain.min + (i / 50) * (xDomain.max - xDomain.min);
                    y = u.yValue(x);
                    if (inRange(y, yDomain)) {
                        points.push({x: x, y: y});
                    }
                }
            }

            return points;
        };

        // Add utility function methods (marginal utility, MRS, etc.)
        u = econgraphs.functions.utility.addUtilityMethods(u);

        u.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            if (params) {

                u.xmin = params['xmin'] || u.xmin;
                u.ymax = params['ymax'] || u.ymax;
                u.alpha = params['alpha'] || u.alpha;
                u.delta = params['delta'] || u.delta;

            }

            return u;

        };

        u.updateParams(params);

        // Find the optimal bundle for a given income and prices
        u._optimalBundle = function (income, px, py) {

            var extremeBundleCost = income - px* u.xmin - py* u.ymax;

            if(extremeBundleCost < 0) {

                var alpha_1 = u.alpha,
                    alpha_2 = u.alpha + u.delta,
                    beta_1 = alpha_1 / (alpha_2 - alpha_1),
                    beta_2 = alpha_2 / (alpha_2 - alpha_1);

                var optimal_x = u.xmin - beta_1 * extremeBundleCost / px,
                    optimal_y = u.ymax + beta_2 * extremeBundleCost / py;

                return [optimal_x, optimal_y];

                if(optimal_x >= u.xmin && optimal_y > 0) {
                    return [optimal_x, optimal_y];
                }

                // Corner solution if buy no y
                if(optimal_y <= 0) {
                    return [income/px, 0]
                }

                // Corner solution if X is as subsistence level
                if(optimal_x < u.xmin) {
                    return [u.xmin, (income - u.xmin * px)/py]
                }

            }

        };

        // Find the lowest possible cost for a given level of utility, given px and py
        u._lowestPossibleCost = function (utility, px, py) {

            return u.xmin*px + u.ymax*py - 0.25/Math.exp(utility); // TODO This is wrong. Needs more math.

        };

        return u;
    }

}();

// production/production.js
/**
 * Created by cmakler on 2/17/15.
 */

econgraphs.functions.production = {

    addProductionMethods: function (f, params) {

        f.longRunTotalCost = function(q,w,r) {
            return f.lowestPossibleCost(q,w,r);
        };

        f.longRunTotalCostCurve = function (w, r) {

            return {
                points: function (xDomain, yDomain) {

                    var longRunTotalCost = function (q) {
                        return f.longRunTotalCost(q, w, r)
                    };

                    return functionPoints(longRunTotalCost, xDomain, yDomain);
                }
            }

        };

        f.laborToProduceQ = function(q,k) {
            f.setLevel(q);
            return f.xValue(k);
        };

        f.shortRunFixedCost = function(r,k) {
            return r*k;
        };

        f.shortRunFixedCostCurve = function (r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunFixedCost = function (q) {
                        return f.shortRunFixedCost(r, k)
                    };

                    return functionPoints(shortRunFixedCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunVariableCost = function(q,w,k) {
            return w* f.laborToProduceQ(q,k);
        };

        f.shortRunVariableCostCurve = function (w, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunVariableCost = function (q) {
                        return f.shortRunVariableCost(q, w, k)
                    };

                    return functionPoints(shortRunVariableCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunTotalCost = function(q,w,r,k) {
            return f.shortRunFixedCost(r,k) + f.shortRunVariableCost(q,w,k);
        };

        f.shortRunTotalCostCurve = function (w, r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunTotalCost = function (q) {
                        return f.shortRunTotalCost(q, w, r, k)
                    };

                    return functionPoints(shortRunTotalCost, xDomain, yDomain);
                }
            }

        };

        f.longRunMarginalCost = function(q,w,r) {
            return (f.longRunTotalCost(q+0.01,w,r) - f.longRunTotalCost(q,w,r))*100;
        };

        f.longRunMarginalCostCurve = function (w, r) {

            return {
                points: function (xDomain, yDomain) {

                    var longRunMarginalCost = function (q) {
                        return f.longRunMarginalCost(q, w, r)
                    };

                    return functionPoints(longRunMarginalCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunMarginalCost = function (q, w, r, k) {
            return (f.shortRunTotalCost(q + 0.01, w, r, k) - f.shortRunTotalCost(q, w, r, k)) * 100;
        };

        f.shortRunMarginalCostCurve = function (w, r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunMarginalCost = function (q) {
                        return f.shortRunMarginalCost(q, w, r, k)
                    };

                    return functionPoints(shortRunMarginalCost, xDomain, yDomain);
                }
            }

        };

        f.longRunAverageCost = function (q, w, r) {
            return f.longRunTotalCost(q, w, r)/q;
        };

        f.longRunAverageCostCurve = function (w, r) {

            return {
                points: function (xDomain, yDomain) {

                    var longRunAverageCost = function (q) {
                        return f.longRunAverageCost(q, w, r)
                    };

                    return functionPoints(longRunAverageCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunAverageCost = function (q, w, r, k) {
            return f.shortRunTotalCost(q, w, r, k) / q;
        };

        f.shortRunAverageCostCurve = function (w, r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunAverageCost = function (q) {
                        return f.shortRunAverageCost(q, w, r, k)
                    };

                    return functionPoints(shortRunAverageCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunAverageFixedCost = function (q, r, k) {
            return r * k / q;
        };

        f.shortRunAverageFixedCostCurve = function (r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunAverageFixedCost = function (q) {
                        return f.shortRunAverageFixedCost(q, r, k)
                    };

                    return functionPoints(shortRunAverageFixedCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunAverageVariableCost = function (q, w, k) {
            return f.shortRunVariableCost(q,w,k) / q;
        };

        f.shortRunAverageVariableCostCurve = function (w, r, k) {

            return {
                points: function (xDomain, yDomain) {

                    var shortRunAverageVariableCost = function (q) {
                        return f.shortRunAverageVariableCost(q, w, r, k)
                    };

                    return functionPoints(shortRunAverageVariableCost, xDomain, yDomain);
                }
            }

        };

        f.shortRunProfit = function(q,p,w,r,k) {
            return p * f.shortRunTotalCost(q,w,r,k);
        };

        f.longRunProfitMaxQ = function(p,w,r) {
            //TODO Currently hard-coded for f(L,K) = (LK)^0.25
            return 0.25*Math.pow(w * r, -0.5) * p;
        };

        f.shortRunProfitMaxQ = function(p,w,r,k) {
            //TODO Currently hard-coded for f(L,K) = (LK)^0.25
            return Math.pow(0.25*p*k/w,0.33333);
        };

        f.shortRunSupplyCurve = function(w,r,k,n) {

            return {
                points: function(xDomain,yDomain) {
                    var shortRunSupplyQuantity = function(p) {
                        return f.shortRunProfitMaxQ(p,w,r,k)*n;
                    };

                    return functionPoints(shortRunSupplyQuantity, xDomain, yDomain, {dependentVariable:'y'})
                }
            }
        };

        f.profitArea = function(p,q,w,r,k) {
            return {
                area: function(xDomain,yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var ac = Math.min(f.shortRunAverageCost(q,w,r,k), yDomain.max),
                        price = Math.min(p, yDomain.max),
                        quantity = Math.min(q, xDomain.max);

                    return [
                        {x:xDomain.min, y:ac},
                        {x:quantity, y:ac},
                        {x:quantity, y:price},
                        {x:xDomain.min, y:price}
                    ]
                }
            }
        };

        return f;

    }

};

// production/cobbdouglasproduction.js
/**
 * Created by cmakler on 2/17/15.
 */

econgraphs.functions.production.CobbDouglas = function () {

    return function (params) {

        var f = new econgraphs.functions.utility.CobbDouglas(params);

        f = econgraphs.functions.production.addProductionMethods(f, params);

        f._shortRunProfitMaxQ = function(p,w,r,k) {
            return 3;
        };

        return f;
    }

}();

// production/translog.js
/**
 * Created by cmakler on 1/26/15.
 *
 * Note: this implements the utility function described in http://www.hindawi.com/journals/isrn/2012/608645/
 */

econgraphs.functions.production.Translog = function () {

    function translogTotalCost(q,w,r,coefficients) {

        var logW = Math.log(w),
            logR = Math.log(r);

        var logQ = Math.log(q),
            variables = [
                1, logQ, logW, logR,
                    logQ * logQ, logW * logW, logR * logR,
                    logR * logW, logW * logQ, logR * logQ
            ],
            sum = 0;
        for (var i = 0; i < 10; i++) {
            sum += coefficients[i] * variables[i];
        }
        return Math.exp(sum);
    }

    return function (params) {

        var f = new kg.functions.Generic;

        f.coefficients = [1,1,1,1,1,1,1,1,1,1];

        f.w = 2;

        f.r = 2;

        f.totalCost = {
            points: function (xDomain, yDomain) {

                function totalCostOfOutput(q) {

                    return translogTotalCost(q, f.w, f.r, f.coefficients)

                }

                return functionPoints(totalCostOfOutput, xDomain, yDomain);

            }
        };

        f.marginalCost = {
            points: function (xDomain, yDomain) {

                function marginalCostOfOutput(q) {

                    return translogTotalCost(q+1, f.w, f.r, f.coefficients) - translogTotalCost(q, f.w, f.r, f.coefficients)

                }

                return functionPoints(marginalCostOfOutput, xDomain, yDomain);

            }
        };

        f.averageCost = {
            points: function (xDomain, yDomain) {

                function averageCostOfOutput(q) {

                    if(q == 0) { q = 0.01 } // avoid dividing by zero

                    return translogTotalCost(q, f.w, f.r, f.coefficients)/q

                }

                return functionPoints(averageCostOfOutput, xDomain, yDomain);

            }
        };

        f.updateParams = function(params) {

            var coefficientMeaning = {
                'constant': 0,
                'logQ' : 1,
                'logW' : 2,
                'logR' : 3,
                'logQ2' : 4,
                'logW2' : 5,
                'logR2' : 6,
                'logRlogW' : 7,
                'logQlogW' : 8,
                'logQlogR' : 9
            };

            if (typeof params == 'function') {
                params = params();
            };

            params = params || {};

            for(var term in coefficientMeaning) {
                if (params.hasOwnProperty(term)) {
                    f.coefficients[coefficientMeaning[term]] = params[term];
                }
            }

            if (params.hasOwnProperty('r')) {
                f.r = params.r;
            }

            if (params.hasOwnProperty('w')) {
                f.w = params.w;
            }

            return f;
        };

        return f;
    }

}();

// market/market.js
/**
 * Created by cmakler on 3/3/15.
 */

econgraphs.functions.market = {

}

// market/lineardemand.js
/**
 * Created by cmakler on 3/3/15.
 */

econgraphs.functions.market.LinearDemand = function () {

    return function (params) {

        var d = new kg.functions.Linear({definitionType: 'slope-intercept'});

        d.qIntercept = 40;
        d.dQdP = -1;

        d.marginalRevenue = new kg.functions.Linear({definitionType: 'slope-intercept'});

        d.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            params = params || {};

            // intercept is quantity demanded at P = 0
            if (params.hasOwnProperty('intercept')) {
                d.qIntercept = params.intercept;
            }

            // slope is dQ/dP
            if (params.hasOwnProperty('slope')) {
                d.dQdP = params.slope;
            }

            // invert the slope and intercept
            d.setIntercept(-d.qIntercept / d.dQdP);
            d.setSlope(1/ d.dQdP);

            // set new slope and intercept for marginal revenue
            d.marginalRevenue.setIntercept(d.intercept);
            d.marginalRevenue.setSlope(2*d.slope);

            return d;

        };

        d.quantityDemanded = function(p) {
            return d.xValue(p);
        };

        d.maximumPrice = function(q) {
            return d.yValue(q);
        };

        d.revenue = function(q) {
            return q* d.maximumPrice(q);
        };

        d.marketPrice = function (supply,tax) {
            return d.linearIntersection(supply,tax).y;
        };

        d.consumerSurplus = function (p) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var q = d.quantityDemanded(p),
                        qmax = d.quantityDemanded(yDomain.max);

                    if(qmax > 0) {
                        return [
                            {x: xDomain.min, y: p},
                            {x: q, y: p},
                            {x: qmax, y: yDomain.max},
                            {x: xDomain.min, y: yDomain.max}
                        ]
                    } else {
                        return [
                            {x: xDomain.min, y: p},
                            {x: q, y: p},
                            {x: xDomain.min, y: d.yIntercept()}
                        ]
                    }
                }
            }
        };

        d.marginalRevenueLoss = function (q1,q2) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var p1 = d.maximumPrice(q1),
                        p2 = d.maximumPrice(q2),
                        right = Math.min(q1,q2,xDomain.max),
                        lowerPrice = Math.min(p1,p2),
                        higherPrice = Math.max(p1,p2),
                        bottom = Math.min(lowerPrice, yDomain.max),
                        top = Math.min(higherPrice, yDomain.max);

                    return [
                        {x: xDomain.min, y: bottom},
                        {x: right, y: bottom},
                        {x: right, y: top},
                        {x: xDomain.min, y: top}
                    ]

                }
            }
        };

        d.marginalRevenueGain = function (q1, q2) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var p1 = d.maximumPrice(q1),
                        p2 = d.maximumPrice(q2),
                        top = Math.min(p1, p2, yDomain.max),
                        lowerQuantity = Math.min(q1, q2),
                        higherQuantity = Math.max(q1, q2),
                        left = Math.min(lowerQuantity, xDomain.max),
                        right = Math.min(higherQuantity, xDomain.max);

                    return [
                        {x: left, y: yDomain.min},
                        {x: right, y: yDomain.min},
                        {x: right, y: top},
                        {x: left, y: top}
                    ]

                }
            }
        };

        d.unchangedRevenue = function (q1, q2) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var quantity = Math.min(q1, q2),
                        price = d.maximumPrice(quantity),
                        top = Math.min(price, yDomain.max),
                        right = Math.min(quantity, xDomain.max);

                    return [
                        {x: xDomain.min, y: yDomain.min},
                        {x: right, y: yDomain.min},
                        {x: right, y: top},
                        {x: xDomain.min, y: top}
                    ]

                }
            }
        };

        d.totalRevenue = function (quantity) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var price = d.maximumPrice(quantity),
                        top = Math.min(price, yDomain.max),
                        right = Math.min(quantity, xDomain.max);

                    return [
                        {x: xDomain.min, y: yDomain.min},
                        {x: right, y: yDomain.min},
                        {x: right, y: top},
                        {x: xDomain.min, y: top}
                    ]

                }
            }
        };

        d.totalRevenueCurve = function () {
            return {
                points: function (xDomain, yDomain) {
                    return functionPoints(d.revenue, xDomain, yDomain);
                }
            }
        };

        d.mrTangentLine = function (q) {
            var point = {x: q, y: d.revenue(q)},
                slope = d.marginalRevenue.yValue(q),
                l = new kg.functions.Linear({definitionType: 'point-slope', point: point, slope: slope});
            return l;
        };

        d.updateParams(params);

        return d;
    }

}();

// market/linearsupply.js
/**
 * Created by cmakler on 3/3/15.
 */

econgraphs.functions.market.LinearSupply = function () {

    return function (params) {

        var s = new kg.functions.Linear({definitionType: 'slope-intercept'});

        var f = new econgraphs.functions.production.CobbDouglas();

        s.setSlope(1);
        s.setIntercept(0);

        // set default values for Cobb Douglas function f(L,K) = sqrt(LK)
        s.w = 1;
        s.r = 100;
        s.k = 1;
        s.n = 2000;

        s.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            params = params || {};

            if (params.hasOwnProperty('w')) {
                s.w = params.w;
            }

            if (params.hasOwnProperty('r')) {
                s.r = params.r;
            }

            if (params.hasOwnProperty('k')) {
                s.k = params.k;
            }

            if (params.hasOwnProperty('n')) {
                s.n = params.n;
            }

            if (params.hasOwnProperty('fixedCost')) {
                s.fixedCost = params.fixedCost;
            }

            // q(P) = PK/2w => Q(P) = Nq(P) = PKN/2w => P(Q) = 2wQ/NK
            s.setSlope((2* s.w)/((s.n/1000) * s.k));

            return s;

        };

        s.quantitySupplied = function(p) {
            return s.xValue(p);
        };

        s.q = function(p) {
            return s.xValue(p)/(s.n/1000)
        };

        //TODO this doesn't currently update
        s.marginalCostCurve =  f.shortRunMarginalCostCurve(s.w, s.r, s.k);

        //TODO this doesn't currently update
        s.averageCostCurve = f.shortRunAverageCostCurve(s.w, s.r, s.k);

        s.profitArea = function(p) {
            return f.profitArea(p, s.q(p), s.w, s.r, s.k)
        };

        s.updateParams(params);

        s.qEquilibrium = function(demand, tax){
            return s.q(s.marketPrice(demand, tax));
        };

        s.marketPrice = function(demand, tax) {
            return s.linearIntersection(demand, tax).y;
        };

        s.profit = function(p) {
            return f.shortRunProfit(s.q(p), p, s.w, s.r, s.k)
        };

        s.producerSurplus = function(p) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var q = s.quantitySupplied(p);

                    return [
                        {x: xDomain.min, y: yDomain.min},
                        {x: q, y: p},
                        {x: xDomain.min, y: p}
                    ]
                }
            }
        };

        s.taxWedge = function(demand,tax) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var pc = Math.min(s.marketPrice(demand,-tax),yDomain.max),
                        pf = Math.min(s.marketPrice(demand,tax),yDomain.max),
                        q = s.quantitySupplied(pf);

                    return [
                        {x: xDomain.min, y:pc},
                        {x: q, y: pc},
                        {x: q, y: pf},
                        {x: xDomain.min, y: pf}
                    ]

                }
            }
        };

        s.taxDWL = function (demand, tax) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var pc = Math.min(s.marketPrice(demand, -tax), yDomain.max),
                        pf = Math.min(s.marketPrice(demand, tax), yDomain.max),
                        qt = s.quantitySupplied(pf),
                        pe = s.marketPrice(demand),
                        qe = s.quantitySupplied(pe);

                    return [
                        {x: qt, y: pc},
                        {x: qe, y: pe},
                        {x: qt, y: pf}

                    ]

                }
            }
        }

        return s;
    }

}();

// market/monopoly.js
/**
 * Created by cmakler on 3/3/15.
 */

econgraphs.functions.market.Monopoly = function () {

    return function (params) {

        var m = {};

        // We're assuming a monopoly's cost structure is defined by its (linear) marginal cost

        m.marginalCost = new kg.functions.Linear({definitionType: 'slope-intercept'});

        m.marginalCost.setSlope(0);
        m.marginalCost.setIntercept(1);

        // If MC =

        m.fixedCost = 12;

        m.updateParams = function (params) {

            if (typeof params == 'function') {
                params = params();
            }

            params = params || {};

            if (params.hasOwnProperty('mcSlope')) {
                m.marginalCost.setSlope(params['mcSlope']);
            }

            if (params.hasOwnProperty('mcIntercept')) {
                m.marginalCost.setIntercept(params['mcIntercept']);
            }

            if (params.hasOwnProperty('fixedCost')) {
                m.fixedCost = params['fixedCost']
            }

            return m;

        };

        m.updateParams(params);

        // total cost is FC plus the integral of marginal cost
        // MC = a + bq
        // TC = FC + aq + 0.5bq^2
        m.totalCost = function (q) {
            return m.fixedCost + q * m.marginalCost.intercept + 0.5 * q * q * m.marginalCost.slope;
        };

        m.averageCost = function (q) {
            return m.totalCost(q)/q;
        };

        m.totalCostCurve = function () {
            return {
                points: function (xDomain, yDomain) {
                    return functionPoints(m.totalCost, xDomain, yDomain);
                }
            }
        };

        m.averageCostCurve = function () {
            return {
                points: function (xDomain, yDomain) {
                    return functionPoints(m.averageCost, xDomain, yDomain);
                }
            }
        };

        m.optimalCondition = function(demand) {
            return m.marginalCost.linearIntersection(demand.marginalRevenue)
        };

        m.optimalOffer = function(demand) {
            return {x: m.Q(demand), y: m.P(demand)};
        };

        m.Q = function(demand) {
            return m.optimalCondition(demand).x;
        };

        m.P = function (demand) {
            return demand.yValue(m.Q(demand));
        };

        m.profit = function(demand) {
            var offer = m.optimalOffer(demand),
                revenue = offer.x * offer.y,
                cost = m.totalCost(offer.x);

            return revenue - cost;
        };

        m.profitArea = function (demand) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var price = Math.min(m.P(demand), yDomain.max),
                        quantity = Math.min(m.Q(demand), xDomain.max),
                        ac = Math.min(m.averageCost(quantity), yDomain.max);

                    return [
                        {x: xDomain.min, y: ac},
                        {x: quantity, y: ac},
                        {x: quantity, y: price},
                        {x: xDomain.min, y: price}
                    ]
                }
            }
        };

        m.producerSurplus = function(demand) {
            return {
                area: function(xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    var price = Math.min(m.P(demand), yDomain.max),
                        quantity = Math.min(m.Q(demand), xDomain.max),
                        mcZero = Math.min(m.marginalCost.yValue(0), yDomain.max),
                        mcQuantity = Math.min(m.marginalCost.yValue(quantity), yDomain.max);

                    return [
                        {x: xDomain.min, y: mcZero},
                        {x: quantity, y: mcQuantity},
                        {x: quantity, y: price},
                        {x: xDomain.min, y: price}
                    ]

                }
            }
        };

        m.deadWeightLoss = function (demand) {
            return {
                area: function (xDomain, yDomain) {

                    xDomain = domainAsObject(xDomain);
                    yDomain = domainAsObject(yDomain);

                    // TODO handle cases where one or more points is off the graph

                    var monopolyPrice = m.P(demand),
                        monopolyQuantity = m.Q(demand),
                        monopolyMC = m.marginalCost.yValue(monopolyQuantity);

                    return [
                        {x: monopolyQuantity, y: monopolyMC},
                        {x: monopolyQuantity, y: monopolyPrice},
                        m.marginalCost.linearIntersection(demand)
                    ]

                }
            }
        };

        return m;
    }

}();


text:
require "simplecov" if ENV["COVERAGE_ENABLED"] && RUBY_VERSION >= "1.9"

lib = File.expand_path("../../lib/", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

RSpec.configure do |config|
  config.mock_with(:rspec) do |mocks|
    mocks.verify_partial_doubles = true
  end
end

redpajama_set_name: RedPajamaGithub


text:
package scalan.demo

import java.io.File
import scalan.BaseNestedTests
import scalan.compilation.{KernelStore, KernelType}
import scalan.examples.Helpers._

class Demo2 extends BaseNestedTests {
  describe("Matrix Vector multiplication (mvm)") {

    val ctxStd = new Example2Std
    import ctxStd._
    it("Standard evaluation") {
      val res = dmdvA((dmData, dvData))

      print("m", dmData)
      printColumn("v", dvData)
      printColumn("r", res)
    }

    it("Staged evaluation") {
      val ctx = new Example2Exp
      import ctx._

      val kernelsDir = new File("./test-out/Demo2/mvm")
      val store = KernelStore.open(ctx, kernelsDir)

      val (dmdvA_s, _) = time("Generating Scala kernel") {
        val k = store.createKernel("dmdvA", KernelType.Scala, dmdvA)
        k((dmData, dvData))
        k
      }

      val (dmdvA_c, _) = time("Generating C++ kernel") {
        val k = store.createKernel("dmdvA", KernelType.Cpp, dmdvA)
        k((dmData, dvData))
        k
      }

      val rows = 10000
      val cols = rows;

      val (m,_) = time( s"""Generating ${rows}x${cols} Array[Array[Double]] ...""") {
        genMatr(rows, cols)
      }

      val (v, _) = time( s"""Generating ${rows} Array[Double] ...""") {
        genArray(cols)
      }

      val (correctRes,_) = time("Calculating expected result...") {
        m.map { r: Array[Double] =>
           r.zip(v)
            .map { case (x,y) => x * y }
            .reduce(_ + _)
        }
      }

      val (resStd,_) = time(s"Standard Evaluation") { ctxStd.dmdvA((m, v)) }
      val (resS,_) = time(s"Executing Scala kernel") { dmdvA_s((m, v)) }
      val (resC,_) = time(s"Executing C++ kernel") { dmdvA_c((m, v)) }

      assertResult(correctRes)(resStd)
      assertResult(correctRes)(resS)
      assertResult(correctRes)(resC)
    }

  }

}


text:
<?php namespace Teepluss\Api;

use Guzzle\Http\Client;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

class ApiServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap classes for packages.
     *
     * @return void
     */
    public function boot()
    {
        $this->package('teepluss/api');

        // Auto create app alias with boot method.
        $loader = AliasLoader::getInstance();
        $loader->alias('API', 'Teepluss\Api\Facades\API');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        // Register providers.
        $this->registerApi();

        // Register commands.
        $this->registerApiCallCommand();

        // Assign commands.
        $this->commands(
            'api.call'
        );
    }

    /**
     * Register Api.
     *
     * @return void
     */
    public function registerApi()
    {
        $this->app['api'] = $this->app->share(function($app)
        {
            $remoteClient = new Client();

            return new Api($app['config'], $app['router'], $app['request'], $remoteClient);
        });
    }

    /**
     * Register Api Call command.
     *
     * @return void
     */
    public function registerApiCallCommand()
    {
        $this->app['api.call'] = $this->app->share(function($app)
        {
            return new Commands\ApiCallCommand($app['api']);
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('api');
    }

}


text:
require File.dirname(__FILE__) + '/../spec_helper'

describe Product do
  it "should save product in spree base from amazon" do
    Product.save_from_amazon({ :attributes =>{
                                 :amazon_id      => "B001TH7GSW",
                                 :sku            => "B001TH7GSW",
                                 :name           => "Toslink Digital Audio Optical Cable",
                                 :count_on_hand  => 10,
                                 :available_on   => 1.day.ago,
                                 :description    => "AmazonBasics products are quality electronics ......",
                                 :price          => 19.9
                               },
                               :price => 19.9,
                               :images =>[ ]
                             })
    @product = Product.find_by_amazon_id("B001TH7GSW")
    @product.should be_present
    @product.name.should == "Toslink Digital Audio Optical Cable"
    @product.price.should == 19.9
  end

end


text:
//
// Revit IFC Import library: this library works with Autodesk(R) Revit(R) to import IFC files.
// Copyright (C) 2013  Autodesk, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB.IFC;
using Revit.IFC.Import.Data;
using Revit.IFC.Common.Utility;
using Revit.IFC.Common.Enums;

namespace Revit.IFC.Import.Utility
{
    /// <summary>
    /// Utilities for processing attributes from handles.
    /// </summary>
    public class IFCImportHandleUtil
    {
        /// <summary>
        /// Finds the value of a required length attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="found">True if it was found, false if not.</param>
        /// <returns>The length value, scaled.</returns>
        static public double GetRequiredScaledLengthAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
            {
                found = true;
                return IFCUnitUtil.ScaleLength(value.Value);
            }

            Importer.TheLog.LogMissingRequiredAttributeError(handle, name, false);
            found = false;
            return 0.0;
        }

        /// <summary>
        /// Finds the value of a optional double attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The double value.</returns>
        static public double GetOptionalDoubleAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
                return value.Value;

            return defaultValue;
        }

        /// <summary>
        /// Finds the value of a optional length attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The length value, scaled.</returns>
        /// <remarks>defaultValue should be scaled.</remarks>
        static public double GetOptionalScaledLengthAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
                return IFCUnitUtil.ScaleLength(value.Value);

            return defaultValue;
        }

        /// <summary>
        /// Finds the value of a optional normalised ratio attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The normalised ratio value.</returns>
        /// <remarks>If the normalised ratio given isn't [0,1], the default value will be used.</remarks>
        static public double GetOptionalNormalisedRatioAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue && (value.Value > -MathUtil.Eps()) && (value.Value < 1.0 + MathUtil.Eps()))
                return value.Value;

            return defaultValue;
        }

        /// <summary>
        /// Finds the value of a optional positive ratio attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The positive ratio value.</returns>
        /// <remarks>If the positive ratio given isn't positive, the default value will be used.</remarks>
        static public double GetOptionalPositiveRatioAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue && (value.Value > MathUtil.Eps()))
                return value.Value;

            return defaultValue;
        }

        /// <summary>
        /// Finds the value of a optional (unitless) real (number) attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The real number value.</returns>
        static public double GetOptionalRealAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
                return value.Value;

            return defaultValue;
        }

        /// <summary>
        /// Finds the value of a required angle attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="found">Returns true if the angle was found.</param>
        /// <returns>The angle, scaled.</returns>
        static public double GetRequiredScaledAngleAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
            {
                found = true;
                return IFCUnitUtil.ScaleAngle(value.Value);
            }

            found = false;
            return 0.0;
        }

        /// <summary>
        /// Finds the value of a optional angle attribute.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The name of the atribute.</param>
        /// <param name="defaultValue">The default value, if not found.</param>
        /// <returns>The length value, scaled.</returns>
        /// <remarks>defaultValue should be scaled.</remarks>
        static public double GetOptionalScaledAngleAttribute(IFCAnyHandle handle, string name, double defaultValue)
        {
            double? value = IFCAnyHandleUtil.GetDoubleAttribute(handle, name);
            if (value.HasValue)
                return IFCUnitUtil.ScaleAngle(value.Value);

            return defaultValue;
        }

        /// <summary>
        /// Get a required instance attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="throwError">Throw if null or invalid.</param>
        /// <returns>The attribute handle.</returns>
        static public IFCAnyHandle GetRequiredInstanceAttribute(IFCAnyHandle handle, string name, bool throwError)
        {
            IFCAnyHandle attribute = IFCAnyHandleUtil.GetInstanceAttribute(handle, name);
            if (IFCAnyHandleUtil.IsNullOrHasNoValue(attribute))
                Importer.TheLog.LogMissingRequiredAttributeError(handle, name, throwError);
            return attribute;
        }

        /// <summary>
        /// Get an optional instance attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <returns>The attribute handle.</returns>
        static public IFCAnyHandle GetOptionalInstanceAttribute(IFCAnyHandle handle, string name)
        {
            return IFCAnyHandleUtil.GetInstanceAttribute(handle, name);
        }

        /// <summary>
        /// Get a required boolean attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <returns>The attribute value.</returns>
        static public bool GetRequiredBooleanAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            bool? attribute = IFCAnyHandleUtil.GetBooleanAttribute(handle, name);
            if (!attribute.HasValue)
            {
                Importer.TheLog.LogMissingRequiredAttributeError(handle, name, false);
                found = false;
                return false;
            }
            found = true;
            return attribute.Value;
        }

        /// <summary>
        /// Get a required integer attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <returns>The attribute value.</returns>
        static public int GetRequiredIntegerAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            int? attribute = IFCAnyHandleUtil.GetIntAttribute(handle, name);
            if (!attribute.HasValue)
            {
                Importer.TheLog.LogMissingRequiredAttributeError(handle, name, false);
                found = false;
                return 0;
            }
            found = true;
            return attribute.Value;
        }

        /// <summary>
        /// Get an optional integer attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <returns>The attribute value.</returns>
        static public int GetOptionalIntegerAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            int? attribute = IFCAnyHandleUtil.GetIntAttribute(handle, name);
            if (!attribute.HasValue)
            {
                found = false;
                return 0;
            }
            found = true;
            return attribute.Value;
        }

        /// <summary>
        /// Get a required string attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="throwError">True if the routine should throw an exception.</param>
        /// <returns>The attribute valu, or null if not found.</returns>
        static public string GetRequiredStringAttribute(IFCAnyHandle handle, string name, bool throwError)
        {
            string attribute = IFCAnyHandleUtil.GetStringAttribute(handle, name);
            if (attribute == null)
                Importer.TheLog.LogMissingRequiredAttributeError(handle, name, throwError);
            return attribute;
        }

        /// <summary>
        /// Get an optional string attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The attribute value, or defaultValue if not found.</returns>
        static public string GetOptionalStringAttribute(IFCAnyHandle handle, string name, string defaultValue)
        {
            string attribute = IFCAnyHandleUtil.GetStringAttribute(handle, name);
            if (attribute == null)
                attribute = defaultValue;
            return attribute;
        }

        /// <summary>
        /// Get an optional logical attribute from an entity.
        /// </summary>
        /// <param name="handle">The entity handle.</param>
        /// <param name="name">The attribute name.</param>
        /// <returns>The attribute value, or IFCLogical.Unknown if not found.</returns>
        static public IFCLogical GetOptionalLogicalAttribute(IFCAnyHandle handle, string name, out bool found)
        {
            IFCLogical? attribute = IFCAnyHandleUtil.GetLogicalAttribute(handle, name);
            if (!attribute.HasValue)
            {
                found = false;
                return IFCLogical.Unknown;
            }
            found = true;
            return attribute.Value;
        }

        /// <summary>
        /// Get attribute type of List of List of Double
        /// </summary>
        /// <param name="handle">the handle</param>
        /// <param name="name">the attribute name</param>
        /// <returns>List of List of Double</returns>
        public static IList<IList<double>> GetListOfListOfDoubleAttribute(IFCAnyHandle handle, string name)
        {
            if (handle == null)
                throw new ArgumentNullException("handle");

            if (!handle.HasValue)
                throw new ArgumentException("Invalid handle.");

            IList<IList<double>> outerList = null;

            IFCData ifcData = handle.GetAttribute(name);

            if (ifcData.PrimitiveType == IFCDataPrimitiveType.Aggregate)
            {
                IFCAggregate outer = ifcData.AsAggregate();
                if (outer != null)
                {
                    outerList = new List<IList<double>>();

                    foreach (IFCData outerVal in outer)
                    {
                        IFCAggregate inner = outerVal.AsAggregate();

                        if (inner != null)
                        {
                            IList<double> innerList = new List<double>();
                            foreach (IFCData innerVal in inner)
                            {
                                innerList.Add(innerVal.AsDouble());
                            }
                            outerList.Add(innerList);
                        }
                    }
                }
            }
            return outerList;
        }

        /// <summary>
        /// Get attribute of type List of List of Integer
        /// </summary>
        /// <param name="handle">the handle</param>
        /// <param name="name">attribute name</param>
        /// <returns>List of List of Integer</returns>
        public static IList<IList<int>> GetListOfListOfIntegerAttribute(IFCAnyHandle handle, string name)
        {
            if (handle == null)
                throw new ArgumentNullException("handle");

            if (!handle.HasValue)
                throw new ArgumentException("Invalid handle.");

            IList<IList<int>> outerList = null;

            IFCData ifcData = handle.GetAttribute(name);

            if (ifcData.PrimitiveType == IFCDataPrimitiveType.Aggregate)
            {
                IFCAggregate outer = ifcData.AsAggregate();
                if (outer != null)
                {
                    outerList = new List<IList<int>>();

                    foreach (IFCData outerVal in outer)
                    {
                        IFCAggregate inner = outerVal.AsAggregate();

                        if (inner != null)
                        {
                            IList<int> innerList = new List<int>();
                            foreach (IFCData innerVal in inner)
                            {
                                innerList.Add(innerVal.AsInteger());
                            }
                            outerList.Add(innerList);
                        }
                    }
                }
            }
            return outerList;
        }

        /// <summary>
        /// Get attribute of type IList of IList of Entity
        /// </summary>
        /// <param name="handle">The handle</param>
        /// <param name="name">attribute name</param>
        /// <returns>IList of IList of Entity</returns>
        public static IList<IList<IFCAnyHandle>> GetListOfListOfInstanceAttribute(IFCAnyHandle handle, string name)
        {
            if (handle == null)
                throw new ArgumentNullException("handle");

            if (!handle.HasValue)
                throw new ArgumentException("Invalid handle.");

            IList<IList<IFCAnyHandle>> outerList = null;

            IFCData ifcData = handle.GetAttribute(name);

            if (ifcData.PrimitiveType == IFCDataPrimitiveType.Aggregate)
            {
                IFCAggregate outer = ifcData.AsAggregate();
                if (outer != null)
                {
                    outerList = new List<IList<IFCAnyHandle>>();

                    foreach (IFCData outerVal in outer)
                    {
                        IFCAggregate inner = outerVal.AsAggregate();

                        if (inner != null)
                        {
                            IList<IFCAnyHandle> innerList = new List<IFCAnyHandle>();
                            foreach (IFCData innerVal in inner)
                            {
                                innerList.Add(innerVal.AsInstance());
                            }
                            outerList.Add(innerList);
                        }
                    }
                }
            }
            return outerList;
        }

        /// <summary>
        /// Read the HasAssignments INVERSE attribute from an IfcObjectDefinition, if it is defined.
        /// </summary>
        /// <param name="ifcObjectDefinition">The handle assumed to be an IfcObjectDefinition.</param>
        /// <returns>The set.</returns>
        /// <remarks>This deals with the issue that the default IFC2x3 EXP file doesn't have this inverse attribute set.</remarks>
        static public ISet<IFCAnyHandle> GetHasAssignments(IFCAnyHandle ifcObjectDefinition)
        {
            HashSet<IFCAnyHandle> hasAssignments = null;
            if (IFCImportFile.TheFile.Options.AllowUseHasAssignments)
            {
                try
                {
                    hasAssignments = IFCAnyHandleUtil.GetAggregateInstanceAttribute
                        <HashSet<IFCAnyHandle>>(ifcObjectDefinition, "HasAssignments");
                }
                catch
                {
                    IFCImportFile.TheFile.Options.AllowUseHasAssignments = false;
                    hasAssignments = null;
                }
            }

            return hasAssignments;
        }
    }
}


text:
package org.drools.compiler;

import java.io.Serializable;

public class Func implements Serializable {
    public static void func() {

    }

    public static void func(final String string) {

    }

    public static void func(final String x,
                            final String y) {

    }
}


text:
CMasternodeConfig masternodeConfig;

void CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex, std::string donationAddress, std::string donationPercent) {
    CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex, donationAddress, donationPercent);
    entries.push_back(cme);
}

bool CMasternodeConfig::read(boost::filesystem::path path) {
    boost::filesystem::ifstream streamConfig(GetMasternodeConfigFile());
    if (!streamConfig.good()) {
        return true; // No masternode.conf file is OK
    }

    for(std::string line; std::getline(streamConfig, line); )
    {
        if(line.empty()) {
            continue;
        }
        std::istringstream iss(line);
        std::string alias, ip, privKey, txHash, outputIndex, donation, donationAddress, donationPercent;
        if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex >> donation)) {
            donationAddress = "";
            donationPercent = "";
            iss.str(line);
            iss.clear();
            if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
                LogPrintf("Could not parse masternode.conf line: %s\n", line.c_str());
                streamConfig.close();
                return false;
            }
        } else {
            size_t pos = donation.find_first_of(":");
            if(pos == string::npos) { // no ":" found
                donationPercent = "100";
                donationAddress = donation;
            } else {
                donationPercent = donation.substr(pos + 1);
                donationAddress = donation.substr(0, pos);
            }
            CBitcoinAddress address(donationAddress);
            if (!address.IsValid()) {
                LogPrintf("Invalid TX address in masternode.conf line: %s\n", line.c_str());
                streamConfig.close();
                return false;
            }
			SoftSetArg("-donationAddress", donationAddress);
			SoftSetArg("-donationPercentage", donationPercent);

        }

        if(Params().NetworkID() == CChainParams::MAIN){
            if(CService(ip).GetPort() != 17170) {
                LogPrintf("Invalid port detected in masternode.conf: %s (must be 17170 for mainnet)\n", line.c_str());
                streamConfig.close();
                return false;
            }
        } else if(CService(ip).GetPort() == 17170) {
            LogPrintf("Invalid port detected in masternode.conf: %s (17170 must be only on mainnet)\n", line.c_str());
            streamConfig.close();
            return false;
        }

        if (!(CService(ip).IsIPv4() && CService(ip).IsRoutable())) {
            LogPrintf("Invalid Address detected in masternode.conf: %s (IPV4 ONLY) \n", line.c_str());
            streamConfig.close();
            return false;
        }

        if(Params().NetworkID() == CChainParams::MAIN){
            if(CService(ip).GetPort() != 17170) {
                LogPrintf("Invalid port detected in masternode.conf: %s (must be 17170 for mainnet)\n", line.c_str());
                streamConfig.close();
                return false;
            }
        } else if(CService(ip).GetPort() == 17170) {
            LogPrintf("Invalid port detected in masternode.conf: %s (17170 must be only on mainnet)\n", line.c_str());
            streamConfig.close();
            return false;
        }

        if (!(CService(ip).IsIPv4() && CService(ip).IsRoutable())) {
            LogPrintf("Invalid Address detected in masternode.conf: %s (IPV4 ONLY) \n", line.c_str());
            streamConfig.close();
            return false;
        }

        add(alias, ip, privKey, txHash, outputIndex, donationAddress, donationPercent);
    }

    streamConfig.close();
    return true;
}


text:
package org.seasar.struts.action;

import java.util.ListIterator;

/**
 * リストイテレータのラッパーです。
 *
 * @author higa
 *
 */
@SuppressWarnings("unchecked")
public class ListIteratorWrapper implements ListIterator {

    /**
     * オリジナルのイテレータです。
     */
    protected ListIterator iterator;

    /**
     * インスタンスを構築します。
     *
     * @param iterator
     *            イテレータ
     */
    public ListIteratorWrapper(ListIterator iterator) {
        this.iterator = iterator;
    }

    public void add(Object o) {
        iterator.add(o);
    }

    public boolean hasNext() {
        return iterator.hasNext();
    }

    public boolean hasPrevious() {
        return iterator.hasPrevious();
    }

    public Object next() {
        return WrapperUtil.convert(iterator.next());
    }

    public int nextIndex() {
        return iterator.nextIndex();
    }

    public Object previous() {
        return WrapperUtil.convert(iterator.previous());
    }

    public int previousIndex() {
        return iterator.previousIndex();
    }

    public void remove() {
        iterator.remove();
    }

    public void set(Object o) {
        iterator.set(o);
    }
}


text:
![Wikipsum](icon.png)

# Wikipsum

Wikipsum is a simple tool to get content from Wikipedia and convert it to Markdown to be
used for testing purpose and replace *Lorem Ipsum*.

This project is ready to build. Just open it in XCode 6 and build it for OS 10.10 or 10.9 targets.


text:
/*
 * package_data_source.hpp
 *
 *  Created on: 08/11/2013
 *      Author: svp
 */

#ifndef CRY_TERRA_FILE_SYSTEM_PACKAGE_DATA_SOURCE_HPP_
#define CRY_TERRA_FILE_SYSTEM_PACKAGE_DATA_SOURCE_HPP_

#include <map>
#include <memory>

#include <boost/iostreams/device/mapped_file.hpp>

#include <cry_terra/file_system/path.hpp>
#include <cry_terra/data_structures/indata_free_list.hpp>
#include <cry_terra/file_system/file_extension.hpp>
#include <cry_terra/file_system/file_stream.hpp>
#include <cry_terra/utilities/hash.hpp>

namespace cry_terra
{

namespace file_system
{

enum class compression_type : std::uint8_t
{
    no_compression = 0,
    python_lzox = 1,
    python_lzoy = 2,

    custom,
};

struct packaged_file_descriptor
{
    std::int64_t path_hash;
    std::uint32_t adler32;
    std::size_t size_before, size_after;
    std::size_t begin;
    compression_type compression;

    file_extension extension;
};

namespace detail
{

struct mmap_binding
{
    mmap_binding();
    ~mmap_binding();

    size_t size;
    int fd;
    void* data;
};

}

class package_data_source
{
public:
    package_data_source() = default;
    package_data_source(const package_data_source&) = default;
    package_data_source(const path& package_path);

    unsigned int num_files() const;

    virtual ~package_data_source();

    std::unique_ptr<package_file_stream> open_file(const hash_type&);
    const packaged_file_descriptor& get_descriptor(const hash_type&);
private:
    indata_free_list<packaged_file_descriptor> file_descriptors_;
    std::map<hash_type, std::size_t> file_index_map_;

    void open_package_(const path& package_path);

    boost::iostreams::mapped_file_source source_;
};

}

}

#endif /* CRY_TERRA_FILE_SYSTEM_PACKAGE_DATA_SOURCE_HPP_ */


text:
/********************************************************************************
 *   Ledger Node JS API
 *   (c) 2016-2017 Ledger
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ********************************************************************************/

function runTest(comm, ledger, timeout) {

    return comm.create_async(timeout, true).then(function (comm) {
        var eth = new ledger.eth(comm);
        return eth.getAddress_async("44'/60'/0'/0'/0").then(function (result) {
            console.log(result);
        })
    })

}

module.exports = runTest;


text:
/** Main app **/

'use strict';

var ng = require('angular');
var config = require('./config');
var constants = require('./constants');
var services = require('./angularservices');
var controllers = require('./angularcontrollers');
var directives = require('./angulardirectives');

var animateModule = require('angular-animate');
var translateModule = require('angular-translate');
var sanitizeModule = require('angular-sanitize');
var blockUIModule = require('angular-block-ui');
var uiRouterModule = require('angular-ui-router');

require('angular-block-ui/dist/angular-block-ui.min.css');

var moduleName = config.configBlock.namespace;

// Declare app level module which depends on services, controllers and directives
ng.module(moduleName, [animateModule, translateModule, sanitizeModule, blockUIModule, uiRouterModule,
    services.servicesModule.name, controllers.controllersModule.name, directives.directivesModule.name,
    'inform', 'inform-exception'/*, 'inform-http-exception'*/
]);

ng.module(moduleName)
  .config(config.configBlock.settings)
  .config(config.configBlock.states)
  .config(config.configBlock.blockUI)
  .config(config.configBlock.inform)
  .config(config.configBlock.hrefSanitize);

ng.module(moduleName)
  .constant('CONSTANTS', constants.constants);

var runBlock = function() {

    // Add code that needs a run block to execute here.
};

ng.module(moduleName)
  .run(runBlock);
  // .run(config.configBlock.modal);


text:
title: "DevCoffee 7"
slug: "devcoffee-7"
category: "devcoffee"
date: "2019-03-26"
time: "7PM"
venue: "finegrind"
city: "St. Catharines"
registration: "https://www.facebook.com/events/1451585778308911/"
---

Come one come all for coffee and conversation! We're meeting at the round tables, towards the back of the cafe.

Please bring cash. Fine Grind does not accept plastic. Parking is free downtown at this time.

See you there!


text:
package net.glowstone.block.blocktype;

import net.glowstone.block.GlowBlock;
import net.glowstone.inventory.MaterialMatcher;
import net.glowstone.inventory.ToolType;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;

public class BlockStone extends BlockNeedsTool {

    @Override
    public MaterialMatcher getNeededMiningTool(GlowBlock block) {
        return ToolType.PICKAXE;
    }

    @NotNull
    @Override
    public Collection<ItemStack> getMinedDrops(GlowBlock block) {
        if (block.getType() == Material.STONE) {
            return Arrays.asList(new ItemStack(Material.COBBLESTONE));
        } else {
            return Arrays.asList(new ItemStack(Material.STONE, 1, block.getData()));
        }
    }
}


text:
/*
 *  This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual
 *  contributors.
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.modules.deciderules;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.archive.modules.CrawlURI;

/**
 * Rule applies the configured decision (default REJECT) for any URI which
 * has a URI-scheme NOT contained in the configured Set.
 *
 * @contributor gojomo
 */
public class SchemeNotInSetDecideRule extends PredicatedDecideRule {
    private static final long serialVersionUID = 3L;

    {
        setDecision(DecideResult.REJECT);
    }

    /**
     * Usual constructor.
     * @param name Name of this DecideRule.
     */
    public SchemeNotInSetDecideRule() {
    }

    /** set of schemes to test URI scheme */
    protected Set<String> schemes = new HashSet<String>();
    {
        // default set are those schemes Heritrix supports in usual configuration
        schemes.addAll(Arrays.asList(new String[] {"http","https","ftp","dns","whois"}));
    }
    public Set<String> getSchemes() {
        return schemes;
    }
    public void setSchemes(Set<String> schemes) {
        this.schemes = schemes;
    }

    /**
     * Evaluate whether given object is over the threshold number of
     * hops.
     */
    @Override
    protected boolean evaluate(CrawlURI uri) {
        return !schemes.contains(uri.getUURI().getScheme());
    }
}


text:
package org.nextime.ion.frontoffice.objectSelector;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nextime.ion.framework.business.Section;
import org.nextime.ion.framework.logger.Logger;

/**
 * Renvoi une collection d'objets Section racines ( dont le parent vaut null )
 */
public class ListRootSections implements ObjectSelector {

    public Collection selectObjects(Hashtable params, HttpServletRequest request, HttpServletResponse response) throws SelectException {
        try {
            //Mapping.begin();
            Vector v = Section.listRootSections();
            //Mapping.rollback();
            return v;
        } catch (Exception e) {
            Logger.getInstance().error("Erreur du SelectObject", this, e);
            throw new SelectException(e.getMessage());
        }
    }

}


text:
{{vm.page}}

    {{season.caption}}

    <button ng-click="vm.getResults(season._links.self.href)" class="btn btn-primary pull-right">Results</button>


text:
rwtut-blur
==========

Following the Ray Wenderlich tutorial on GPUImage iOS7 Blur Tutorial