return getRandomNumber() #******************************************************************************* # # getRandomInteger # #******************************************************************************* def getRandomInteger(n): return randrange(n) @oneArgFunctionEvaluator() @argValidator([IntValidator(0)]) def getRandomIntegerOperator(n): return getRandomInteger(n) #******************************************************************************* # # getMultipleRandoms # #******************************************************************************* def getMultipleRandoms(n): ''' Returns n random numbers. '''
# #****************************************************************************** @oneArgFunctionEvaluator( ) @argValidator( [ DateTimeValidator( ) ] ) def getHebrewCalendarDateOperator( n ): return list( hebrew.from_gregorian( n.year, n.month, n.day ) ) #****************************************************************************** # # convertHebrewDateOperator # #****************************************************************************** @argValidator( [ IntValidator( 1 ), IntValidator( 1, 13 ), IntValidator( 1, 30 ) ] ) def convertHebrewDateOperator( year, month, day ): return RPNDateTime( *hebrew.to_gregorian( int( year ), int( month ), int( day ) ), dateOnly = True ) #****************************************************************************** # # getHebrewCalendarDateNameOperator # #****************************************************************************** @oneArgFunctionEvaluator( ) @argValidator( [ DateTimeValidator( ) ] ) def getHebrewCalendarDateNameOperator( n ): date = hebrew.from_gregorian( n.year, n.month, n.day )
#******************************************************************************* @listAndOneArgFunctionEvaluator( ) @argValidator( [ ListValidator( ), ComplexValidator( ) ] ) def evaluatePolynomialOperator( a, b ): return polyval( a, b ) #******************************************************************************* # # exponentiatePolynomialOperator # #******************************************************************************* @listAndOneArgFunctionEvaluator( ) @argValidator( [ ListValidator( ), IntValidator( 1 ) ] ) def exponentiatePolynomialOperator( n, k ): '''Exponentiates an arbitrary polynomial by an integral power k.''' result = n for _ in arange( 0, k - 1 ): result = multiplyPolynomials( result, n ) return result #******************************************************************************* # # solvePolynomialOperator # #*******************************************************************************
except TypeError: raise ValueError( 'expected time value' ) except OSError: raise ValueError( 'out of range error' ) return result #****************************************************************************** # # convertFromUnixTimeOperator # #****************************************************************************** @oneArgFunctionEvaluator( ) @argValidator( [ IntValidator( ) ] ) def convertFromUnixTimeOperator( n ): try: result = RPNDateTime.parseDateTime( int( n ) ).getLocalTime( ) except OverflowError: raise ValueError( 'out of range error' ) except TypeError: raise ValueError( 'expected time value' ) except OSError: raise ValueError( 'out of range error' ) return result #****************************************************************************** #
# getRegularPolygonArea # # based on having sides of edge length k # # http://www.mathopenref.com/polygonregulararea.html # #****************************************************************************** def getRegularPolygonArea(n, k): return multiply(fdiv(n, fmul(4, tan(fdiv(pi, n)))), getPower(k, 2)).convert('meter^2') @twoArgFunctionEvaluator() @argValidator([IntValidator(3), LengthValidator()]) def getRegularPolygonAreaOperator(n, k): return getRegularPolygonArea(n, k) #****************************************************************************** # # getKSphereRadiusOperator # # n - measurement # k - dimension # # n needs to be an RPNMeasurement so getKSphereRadius can tell if it's an # area or a volume and use the correct formula. # #******************************************************************************
#****************************************************************************** def getNthGeneralizedPolygonalNumber(n, k): negative = (fmod(n, 2) == 0) n = floor(fdiv(fadd(n, 1), 2)) if negative: n = fneg(n) return getNthPolygonalNumber(n, k) @twoArgFunctionEvaluator() @argValidator([IntValidator(0), IntValidator(3)]) def getNthGeneralizedPolygonalNumberOperator(n, k): return getNthGeneralizedPolygonalNumber(n, k) #****************************************************************************** # # getNthGeneralizedTriangulartNumberOperator # #****************************************************************************** def getNthGeneralizedTriangularNumber(n): return getNthGeneralizedPolygonalNumber(n, 3)
@argValidator([MultiplicativeValidator()]) def getCubeSuperRootOperator(n): '''Returns the positive, real cube super root of n.''' value = fmul(2, log(n)) return sqrt(fdiv(value, lambertw(value))) #****************************************************************************** # # getSuperRootOperator # #****************************************************************************** @twoArgFunctionEvaluator() @argValidator([MultiplicativeValidator(), IntValidator(1)]) def getSuperRootOperator(n, k): '''Returns the positive, real kth super root of n.''' k = fsub(k, 1) value = fmul(k, log(n)) return root(fdiv(value, lambertw(value)), k) #****************************************************************************** # # getSuperRootsOperator # #****************************************************************************** @twoArgFunctionEvaluator()
if g.useYAFU and n > g.minValueForYAFU: result = runYAFU(n) else: result = factorise(int(n)) if n > g.minValueToCache: if g.ignoreCache or (not g.ignoreCache and n not in g.factorCache): debugPrint('\ncaching factors of ', n, result) g.factorCache[n] = result return result @oneArgFunctionEvaluator() @argValidator([IntValidator()]) def getFactorsOperator(n): return getFactors(n) #****************************************************************************** # # getFactorList # #****************************************************************************** def getFactorList(n): return list(collections.Counter(getFactors(n)).items())
if mp.dps < precision: mp.dps = precision result = 0 for k in arange(0, n + 1): result = fadd( result, fmul(power(binomial(n, k), 2), power(binomial(fadd(n, k), k), 2))) return result @oneArgFunctionEvaluator() @argValidator([IntValidator(0)]) def getNthAperyNumberOperator(n): return getNthAperyNumber(n) #****************************************************************************** # # getNthDelannoyNumberOperator # #****************************************************************************** @cachedFunction('delannoy') def getNthDelannoyNumber(n): if n == 1: return 3
#****************************************************************************** # # getGCD # #****************************************************************************** def getGCD( n, k ): while k: n, k = k, fmod( n, k ) return n @twoArgFunctionEvaluator( ) @argValidator( [ IntValidator( 0 ), IntValidator( 0 ) ] ) def getGCDOperator( n, k ): return getGCD( n, k ) #****************************************************************************** # # getGCDOfListOperator # #****************************************************************************** def getGCDOfList( args ): if isinstance( args, RPNGenerator ): args = list( args ) if not isinstance( args, list ):
def getDigitList(n, dropZeroes=False): result = [] for c in getMPFIntegerAsString(n): if dropZeroes and c == '0': continue result.append(int(c)) return result @oneArgFunctionEvaluator() @argValidator([IntValidator()]) def getDigitsOperator(n): return getDigitList(n) @oneArgFunctionEvaluator() @argValidator([IntValidator()]) def getNonzeroDigitsOperator(n): return getDigitList(n, dropZeroes=True) #****************************************************************************** # # getDecimalDigitListOperator # #******************************************************************************
@oneArgFunctionEvaluator() @argValidator([StringValidator()]) def rollDiceOperator(expression): values, modifier = evaluateDiceExpression(parseDiceExpression(expression)) return sum(values) + modifier #****************************************************************************** # # rollMultipleDice # #****************************************************************************** @twoArgFunctionEvaluator() @argValidator([StringValidator(), IntValidator(0)]) def rollMultipleDice(expression, times): dice = parseDiceExpression(expression) for _ in arange(0, times): values, modifier = evaluateDiceExpression(dice) yield sum(values) + modifier def rollMultipleDiceOperator(n, k): return RPNGenerator(rollMultipleDice(n, k)) #****************************************************************************** # # enumerateDice
else: digits.append(ord(i) - ordA + 36) else: digits = convertToBaseN(n, base, outputBaseDigits=True) result = [] for digit in digits: if dropZeroes and digit == 0: continue result.append(digit) return result def getBaseKDigits(n, k): return getBaseKDigitList(n, k) @twoArgFunctionEvaluator() @argValidator([IntValidator(), IntValidator(2)]) def getBaseKDigitsOperator(n, k): return getBaseKDigitList(n, k) @twoArgFunctionEvaluator() @argValidator([IntValidator(), IntValidator(2)]) def getNonzeroBaseKDigitsOperator(n, k): return getBaseKDigitList(n, k, dropZeroes=True)
loadAstronomyData( ) if isinstance( n, RPNDateTime ): n = n.year if not g.astroDataAvailable: raise ValueError( 'Astronomy functions are unavailable.' ) times, _ = almanac.find_discrete( g.timescale.utc( n, 1, 1 ), g.timescale.utc( n, 12, 31 ), almanac.seasons( g.ephemeris ) ) result = RPNDateTime.parseDateTime( times[ season ].utc_datetime( ) ) return result.getLocalTime( ) @twoArgFunctionEvaluator( ) @argValidator( [ IntValidator( 0 ), IntValidator( 0, 3 ) ] ) def getSeasonOperator( n, season ): getSeason( n, season ) #****************************************************************************** # # getVernalEquinox # #****************************************************************************** @oneArgFunctionEvaluator( ) @argValidator( [ YearValidator( 0 ) ] ) def getVernalEquinoxOperator( n ): '''Returns the date of the vernal equinox for year n.''' return getSeason( n, 0 )
def convertToSignedInt(n, k): newPrecision = (int(k) * 3 / 10) + 3 if mp.dps < newPrecision: setAccuracy(newPrecision) value = fadd(n, (power(2, fsub(k, 1)))) value = fmod(value, power(2, k)) value = fsub(value, (power(2, fsub(k, 1)))) return value @twoArgFunctionEvaluator() @argValidator([IntValidator(), IntValidator(1)]) def convertToSignedIntOperator(n, k): return convertToSignedInt(n, k) @oneArgFunctionEvaluator() @argValidator([IntValidator()]) def convertToCharOperator(n): return convertToSignedInt(n, 8) @oneArgFunctionEvaluator() @argValidator([IntValidator()]) def convertToShortOperator(n): return convertToSignedInt(n, 16)