Coverage for src/lsqfitgp/_gvarext/__init__.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-15 19:54 +0000

1# lsqfitgp/_gvarext/__init__.py 

2# 

3# Copyright (c) 2023, Giacomo Petrillo 

4# 

5# This file is part of lsqfitgp. 

6# 

7# lsqfitgp is free software: you can redistribute it and/or modify 

8# it under the terms of the GNU General Public License as published by 

9# the Free Software Foundation, either version 3 of the License, or 

10# (at your option) any later version. 

11# 

12# lsqfitgp is distributed in the hope that it will be useful, 

13# but WITHOUT ANY WARRANTY; without even the implied warranty of 

14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

15# GNU General Public License for more details. 

16# 

17# You should have received a copy of the GNU General Public License 

18# along with lsqfitgp. If not, see <http://www.gnu.org/licenses/>. 

19 

20import contextlib 1abcdef

21 

22import gvar 1abcdef

23 

24from ._jacobian import jacobian, from_jacobian 1abcdef

25from ._tabulate import tabulate_together 1abcdef

26from ._ufunc import gvar_gufunc 1abcdef

27from ._format import uformat, fmtspec_kwargs, gvar_format 1abcdef

28 

29@contextlib.contextmanager 1abcdef

30def switchgvar(): 1abcdef

31 """ 

32 Context manager to keep new gvars in a separate pool. 

33 

34 Creating new primary gvars fills up memory permanently. This context manager 

35 keeps the gvars created within its context in a separate pool that is freed 

36 when all such gvars are deleted. They can not be mixed in operations with 

37 other gvars created outside of the context. 

38  

39 Returns 

40 ------- 

41 gvar : gvar.GVarFactory 

42 The new gvar-creating function that uses a new pool. The change is also 

43 reflected in the global `gvar.gvar`. 

44 

45 See also 

46 -------- 

47 gvar.switch_gvar, gvar.restore_gvar 

48 

49 Examples 

50 -------- 

51 

52 >>> x = gvar.gvar(0, 1) 

53 >>> with lgp.switchgvar(): 

54 >>> y = gvar.gvar(0, 1) 

55 >>> z = gvar.gvar(0, 1) 

56 >>> w = gvar.gvar(0, 1) 

57 >>> q = y + z # allowed, y and z created in the same pool 

58 >>> p = x + w # allowed, x and w created in the same pool 

59 >>> h = x + y # x and y created in different pools: this will silently 

60 ... # fail and possibly crash python immediately or later on 

61 

62 """ 

63 try: 1ab

64 yield gvar.switch_gvar() 1ab

65 finally: 

66 gvar.restore_gvar() 1ab