Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

from __future__ import print_function, division 

 

from sympy.core.singleton import S 

from sympy.core.function import Function 

from sympy.core import Add 

from sympy.core.evalf import get_integer_part, PrecisionExhausted 

from sympy.core.numbers import Integer 

from sympy.core.relational import Gt, Lt, Ge, Le 

from sympy.core.symbol import Symbol 

 

 

############################################################################### 

######################### FLOOR and CEILING FUNCTIONS ######################### 

############################################################################### 

 

 

class RoundFunction(Function): 

"""The base class for rounding functions.""" 

 

@classmethod 

def eval(cls, arg): 

from sympy import im 

if arg.is_integer: 

return arg 

if arg.is_imaginary or (S.ImaginaryUnit*arg).is_real: 

i = im(arg) 

if not i.has(S.ImaginaryUnit): 

return cls(i)*S.ImaginaryUnit 

return cls(arg, evaluate=False) 

 

v = cls._eval_number(arg) 

if v is not None: 

return v 

 

# Integral, numerical, symbolic part 

ipart = npart = spart = S.Zero 

 

# Extract integral (or complex integral) terms 

terms = Add.make_args(arg) 

 

for t in terms: 

if t.is_integer or (t.is_imaginary and im(t).is_integer): 

ipart += t 

elif t.has(Symbol): 

spart += t 

else: 

npart += t 

 

if not (npart or spart): 

return ipart 

 

# Evaluate npart numerically if independent of spart 

if npart and ( 

not spart or 

npart.is_real and (spart.is_imaginary or (S.ImaginaryUnit*spart).is_real) or 

npart.is_imaginary and spart.is_real): 

try: 

r, i = get_integer_part( 

npart, cls._dir, {}, return_ints=True) 

ipart += Integer(r) + Integer(i)*S.ImaginaryUnit 

npart = S.Zero 

except (PrecisionExhausted, NotImplementedError): 

pass 

 

spart += npart 

if not spart: 

return ipart 

elif spart.is_imaginary or (S.ImaginaryUnit*spart).is_real: 

return ipart + cls(im(spart), evaluate=False)*S.ImaginaryUnit 

else: 

return ipart + cls(spart, evaluate=False) 

 

def _eval_is_finite(self): 

return self.args[0].is_finite 

 

def _eval_is_real(self): 

return self.args[0].is_real 

 

def _eval_is_integer(self): 

return self.args[0].is_real 

 

 

class floor(RoundFunction): 

""" 

Floor is a univariate function which returns the largest integer 

value not greater than its argument. However this implementation 

generalizes floor to complex numbers. 

 

Examples 

======== 

 

>>> from sympy import floor, E, I, Float, Rational 

>>> floor(17) 

17 

>>> floor(Rational(23, 10)) 

2 

>>> floor(2*E) 

5 

>>> floor(-Float(0.567)) 

-1 

>>> floor(-I/2) 

-I 

 

See Also 

======== 

 

sympy.functions.elementary.integers.ceiling 

 

References 

========== 

 

.. [1] "Concrete mathematics" by Graham, pp. 87 

.. [2] http://mathworld.wolfram.com/FloorFunction.html 

 

""" 

_dir = -1 

 

@classmethod 

def _eval_number(cls, arg): 

if arg.is_Number: 

if arg.is_Rational: 

return Integer(arg.p // arg.q) 

elif arg.is_Float: 

return Integer(int(arg.floor())) 

else: 

return arg 

elif isinstance(arg, ceiling): 

return arg 

elif isinstance(arg, floor): 

return arg 

if arg.is_NumberSymbol: 

return arg.approximation_interval(Integer)[0] 

 

def _eval_nseries(self, x, n, logx): 

r = self.subs(x, 0) 

args = self.args[0] 

args0 = args.subs(x, 0) 

if args0 == r: 

direction = (args - args0).leadterm(x)[0] 

if direction.is_positive: 

return r 

else: 

return r - 1 

else: 

return r 

 

def __le__(self, other): 

if self.args[0] == other and other.is_real: 

return S.true 

return Le(self, other, evaluate=False) 

 

def __gt__(self, other): 

if self.args[0] == other and other.is_real: 

return S.false 

return Gt(self, other, evaluate=False) 

 

 

class ceiling(RoundFunction): 

""" 

Ceiling is a univariate function which returns the smallest integer 

value not less than its argument. Ceiling function is generalized 

in this implementation to complex numbers. 

 

Examples 

======== 

 

>>> from sympy import ceiling, E, I, Float, Rational 

>>> ceiling(17) 

17 

>>> ceiling(Rational(23, 10)) 

3 

>>> ceiling(2*E) 

6 

>>> ceiling(-Float(0.567)) 

0 

>>> ceiling(I/2) 

I 

 

See Also 

======== 

 

sympy.functions.elementary.integers.floor 

 

References 

========== 

 

.. [1] "Concrete mathematics" by Graham, pp. 87 

.. [2] http://mathworld.wolfram.com/CeilingFunction.html 

 

""" 

_dir = 1 

 

@classmethod 

def _eval_number(cls, arg): 

if arg.is_Number: 

if arg.is_Rational: 

return -Integer(-arg.p // arg.q) 

elif arg.is_Float: 

return Integer(int(arg.ceiling())) 

else: 

return arg 

elif isinstance(arg, ceiling): 

return arg 

elif isinstance(arg, floor): 

return arg 

if arg.is_NumberSymbol: 

return arg.approximation_interval(Integer)[1] 

 

def _eval_nseries(self, x, n, logx): 

r = self.subs(x, 0) 

args = self.args[0] 

args0 = args.subs(x, 0) 

if args0 == r: 

direction = (args - args0).leadterm(x)[0] 

if direction.is_positive: 

return r + 1 

else: 

return r 

else: 

return r 

 

def __lt__(self, other): 

if self.args[0] == other and other.is_real: 

return S.false 

return Lt(self, other, evaluate=False) 

 

def __ge__(self, other): 

if self.args[0] == other and other.is_real: 

return S.true 

return Ge(self, other, evaluate=False) 

 

 

class frac(Function): 

r"""Represents the fractional part of x 

 

For real numbers it is defined [1]_ as 

 

.. math:: 

x - \lfloor{x}\rfloor 

 

Examples 

======== 

 

>>> from sympy import Symbol, frac, Rational, floor, ceiling, I 

>>> frac(Rational(4, 3)) 

1/3 

>>> frac(-Rational(4, 3)) 

2/3 

 

returns zero for integer arguments 

 

>>> n = Symbol('n', integer=True) 

>>> frac(n) 

0 

 

rewrite as floor 

 

>>> x = Symbol('x') 

>>> frac(x).rewrite(floor) 

x - floor(x) 

 

for complex arguments 

 

>>> r = Symbol('r', real=True) 

>>> t = Symbol('t', real=True) 

>>> frac(t + I*r) 

I*frac(r) + frac(t) 

 

See Also 

======== 

 

sympy.functions.elementary.integers.floor 

sympy.functions.elementary.integers.ceiling 

 

References 

=========== 

 

.. [1] http://en.wikipedia.org/wiki/Fractional_part 

.. [2] http://mathworld.wolfram.com/FractionalPart.html 

 

""" 

@classmethod 

def eval(cls, arg): 

from sympy import AccumBounds, im 

 

def _eval(arg): 

if arg is S.Infinity or arg is S.NegativeInfinity: 

return AccumBounds(0, 1) 

if arg.is_integer: 

return S.Zero 

if arg.is_number: 

if arg is S.NaN: 

return S.NaN 

elif arg is S.ComplexInfinity: 

return None 

else: 

return arg - floor(arg) 

return cls(arg, evaluate=False) 

 

terms = Add.make_args(arg) 

real, imag = S.Zero, S.Zero 

for t in terms: 

# Two checks are needed for complex arguments 

# see issue-7649 for details 

if t.is_imaginary or (S.ImaginaryUnit*t).is_real: 

i = im(t) 

if not i.has(S.ImaginaryUnit): 

imag += i 

else: 

real += t 

else: 

real += t 

 

real = _eval(real) 

imag = _eval(imag) 

return real + S.ImaginaryUnit*imag 

 

def _eval_rewrite_as_floor(self, arg): 

return arg - floor(arg)