public static double density(double x, double shape, double scale) {

        /*!* #ifdef IEEE_754 /*4!*/
        if (Double.isNaN(x) || Double.isNaN(shape) || Double.isNaN(scale)) return x + shape + scale;
        /*!* #endif /*4!*/
        if (shape <= 0 || scale <= 0) { throw new java.lang.ArithmeticException("Math Error: DOMAIN");
        //    	return Double.NaN;
        }
        if (x < 0) return 0;
        if (x == 0) {
            if (shape < 1) { throw new java.lang.ArithmeticException("Math Error: RANGE");
            //    	    return Double.POSITIVE_INFINITY;
            }
            if (shape > 1) { return 0; }
            return 1 / scale;
        }
        x = x / scale;
        /*!*     return exp((shape - 1) * log(x) - lgammafn(shape) - x) / scale; *!*/
        return java.lang.Math.exp((shape - 1) * java.lang.Math.log(x) - misc.lgammafn(shape) - x) / scale;
    }