
library(pracma)

# Basic functions for the Tetration distributions (TD, ITD and OTD)
# It is a work in progress, with no guarantee.
# A package is to be made available in the forthcoming months, cowritten with my colleague Frédéric Bertrand.
# Feel free to report blatant errors or suggestions at yann.dijoux@utt.fr
# Yann Dijoux, M2S Team, Université de Technologie de Troyes, May 9th, 2020







PhiCont<- function(z,gam)
{
  stopifnot(is.numeric(c(z,gam)))
  Q<-rbind(c(0.500000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-0.0833333333333333,0.250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(0.0208333333333333,-0.104166666666667,0.125000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-0.00555555555555642,0.0416666666666679,-0.0902777777777777,0.0625000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(0.00127314814816160,-0.0157986111111015,0.0515046296296404,-0.0668402777777750,0.0312500000000004,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-0.000148809523778937,0.00526620370345654,-0.0258680555555202,0.0506365740740762,-0.0453125000000050,0.0156250000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-4.54695771168190e-05,-0.00133308531849252,0.0113136574059354,-0.0321180555561114,0.0434317129627857,-0.0290364583333371,0.00781249999999911,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(1.99790465558181e-05,0.000158110109623522,-0.00405953759036493,0.0174816743892734,-0.0336202739281362,0.0340108989199734,-0.0178943452380764,0.00390624999999822,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(1.13215937744826e-05,1.98300695046783e-05,0.00106265651993454,-0.00798717301222496,0.0220179880561773,-0.0313159079123579,0.0249550471262410,-0.0107166108629997,0.00195312500000266,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-1.13206915557384e-05,2.81203538179398e-05,-0.000188057310879231,0.00292184669524431,-0.0121852848678827,0.0240907720872201,-0.0267906803492224,0.0174372597357433,-0.00627919353490825,0.000976562499999112,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-1.76951289176941e-06,-3.98308038711548e-05,9.28491353988648e-05,-0.000857435166835785,0.00558566674590111,-0.0156721975654364,0.0237718045245856,-0.0214808784949128,0.0117297621782200,-0.00361631686075725,0.000488281249988454,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(7.21216201782227e-06,2.38418579101563e-06,-9.96589660644531e-05,0.000321388244628906,-0.00214344263076782,0.00857207179069519,-0.0178075991570950,0.0216702790930867,-0.0163663738640025,0.00765414017223520,-0.00205391667668664,0.000244140624994671,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-7.62939453125000e-06,-1.52587890625000e-05,-2.28881835937500e-05,-0.000265121459960938,0.000832557678222656,-0.00399208068847656,0.0112962722778320,-0.0184075683355331,0.0185526404529810,-0.0119658703915775,0.00487200206407579,-0.00115322830583864,0.000122070312475131,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(4.57763671875000e-05,0.000488281250000000,-0.000976562500000000,0.000671386718750000,-0.000488281250000000,0.00173187255859375,-0.00622558593750000,0.0132861137390137,-0.0176562368869782,0.0150940008461475,-0.00845607509836555,0.00303759692906169,-0.000641314146605509,6.10351563210543e-05,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-0.00219726562500000,0.0439453125000000,0.0644531250000000,0.0498046875000000,0.00244140625000000,0.00463867187500000,0.00415039062500000,-0.00770568847656250,0.0142803192138672,-0.0159293413162231,0.0117721408605576,-0.00580776389688253,0.00186104120803066,-0.000353733671090595,3.05175781676326e-05,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(-0.125000000000000,-0.187500000000000,0.0937500000000000,-0.718750000000000,-0.0468750000000000,-0.0156250000000000,0.0234375000000000,0.00170898437500000,-0.0101318359375000,0.0141830444335938,-0.0136723518371582,0.00885996222496033,-0.00389338564127684,0.00112325476948172,-0.000193744228454307,1.52587891655287e-05,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),c(4.12500000000000,15.5000000000000,57.5000000000000,47,-3.50000000000000,2.37500000000000,1.31250000000000,0.820312500000000,0.00976562500000000,-0.0168457031250000,0.0131683349609375,-0.0112361907958984,0.00646859407424927,-0.00255608744919300,0.000669227913022041,-0.000105469729533070,7.62939440335231e-06,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
  
  #Q=sym('matrix([[1/2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-1/12,1/4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[1/48,-5/48,1/8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-1/180,1/24,-13/144,1/16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[11/8640,-91/5760,89/1728,-77/1152,1/32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-1/6720,91/17280,-149/5760,175/3456,-29/640,1/64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-11/241920,-43/32256,391/34560,-37/1152,1501/34560,-223/7680,1/128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[29/1451520,17/107520,-2357/580608,725/41472,-13943/414720,2821/82944,-481/26880,1/256,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[493/43545600,1727/87091200,23137/21772800,-55649/6967296,91313/4147200,-19481/622080,16099/645120,-4609/430080,1/512,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-2711/239500800,4897/174182400,-8189/43545600,2035739/696729600,-1061231/87091200,399637/16588800,-933293/34836480,89993/5160960,-4861/774144,1/1024,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-6203/3592512000,-130741/3284582400,64733/696729600,-1433809/1672151040,116750327/20901888000,-9359389/597196800,24843781/1045094400,-11973091/557383680,363221/30965760,-55991/15482880,1/2048,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[2636317/373621248000,63577/57480192000,-2239199/22992076800,1273/3981312,-29866343/13934592000,4266041/497664000,-74442451/4180377600,647071/29859840,-27367049/1672151040,126973/16588800,-58301/28385280,1/4096,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-10597579/10461394944000,213753497/8369115955200,279772193/14485008384000,-11619857/52553318400,428111767/501645312000,-6357277/1592524800,19832780791/1755758592000,-24624331/1337720832,1116821941/60197437440,-2286713/191102976,12446383/2554675200,-785633/681246720,1/8192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-439018457/78460462080000,-794746111/94152554496000,6393337759/112983065395200,35537219/482833612800,-2839616587/5912248320000,134719883/75246796800,-64790321329/10534551552000,23325578729/1755758592000,-3985756651/225740390400,567886891/37623398400,-3110763437/367873228800,186241757/61312204800,-811373/1265172480,1/16384,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[1165403153/418455797760000,-18308628193/836911595520000,-23258180609/753220435968000,829996184609/9038645231616000,51618621157/257511260160000,-90483301567/94595973120000,1563568997/501645312000,-696512273519/84276412416000,120491069611/8427641241600,-575630579491/36118462464000,2887087013/245248819200,-2441745683/420426547200,65927053/35424829440,-835397/2361655296,1/32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[16962090521/3048749383680000,7385618341/456497233920000,-151024507477/2824576634880000,-83738772653/1095593361408000,19520034930961/180772904632320000,198954426107/441447874560000,-878183896307/515022520320000,52808710793/11236854988800,-2528532481019/252829237248000,2414170227799/168552824832000,-494053042327/36118462464000,22350606031/2522559283200,-4654814159/1195587993600,16881083/15028715520,-1715839/8856207360,1/65536,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-105428488301/19207121117184000,661956220729/28454994247680000,4994078171191/90386452316160000,-774513981877/7747410198528000,-151685845919/1042920603648000,4620953273/70431001804800,16246084798453/18540810731520000,-5045732697827/1854081073152000,6397811878231/1011316948992000,-21108675976223/1896219279360000,451375513399711/33373459316736000,-5364435394487/476763704524800,2970031525261/459105789542400,-684537562649/267811710566400,765925291/1144494489600,-29889983/283398635520,1/131072,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-25128411629/3754478407680000,-2248290160573/72026704189440000,1682994581928941/27658254408744960000,6521262027559/46484461191168000,-334725058668863/2169274855587840000,-4713944985097/21129300541440000,-369242624074237/4338549711175680000,7195953480017/4767637045248000,-786914865389077/200240755900416000,141435211026857/18203705081856000,-351041363612357/30339508469760000,347613751419779/28605822271488000,-24585770222869/2754634737254400,454966805173/98884323901440,-91943627383/55794106368000,93848307067/238054853836800,-10190221/178436177920,1/262144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[34593427483579/2979063683481600000,-1558399584397531/53081498360217600000,-60741669331959547/553165088174899200000,13294165308321041/110633017634979840000,2905253427016163/10012037795020800000,-2556425111410147/12395856317644800000,-71968038879870487/260312982670540800000,-3399288800093093/8677099422351360000,9387862422590369/4004815118008320000,-3863647757023/743008370688000,7085339373659549/800963023601664000,-1879848832667/165542952960000,15565548545612113/1487502758117376000,-5815023174019/847579919155200,616703846436683/192824431607808000,-1066802930299/1020235087872000,1861013561833/8093865030451200,-197698279/6423702405120,1/524288,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[5139346626307111/557355732782284800000,1007259641559602267/14714191345452318720000,-2935998434814863927/36785478363630796800000,-1238904030636031289/4224169764244684800000,8836387440215972869/46465867406691532800000,5218252383023951/10142064259891200000,-341379731539434787/1366643159020339200000,-171447651286072673/662614864979558400000,-53700771833163193/60739695956459520000,18184128639242771/5461111524556800000,-76650479232178637/12014445354024960000,20642813462831521/2184444609822720000,-331544633378061497/31237557920464896000,1027785634801631/118324083032064000,-659581528775581/128549621071872000,43650606087629/20033707180032000,-1270828030841/1942527607308288,35209370953/264890128269312,-201578155/12205034569728,1/1048576,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-252968859037883917/8992005822220861440000,2636299256093088023/64742441919990202368000,823778051898431169719/3237122095999510118400000,-8660944061926017623/54329321890900869120000,-781939978078208249/1214793919129190400000,45483195470882176603/185863469626766131200000,3537951243239146133/4373258108865085440000,-10163942596007270621/34986064870920683520000,-159341904531105467/1325229729959116800000,-6208154157069580241/3975689189877350400000,160705211456722109/36710805248409600000,-1409401622973877481/192231125664399360000,5973039765416452891/624751158409297920000,-16637344987420875173/1749303243546034176000,1454164093636696903/208250386136432640000,-138669055159276933/37022290868699136000,25491572318342761/17482748465774592000,-10871918061421/26896536101191680,30919721783/405582687240192,-13684885/1549845659648,1/2097152,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-32760167675994895967/2505657006999620812800000,-2863039176306849011/16382196842102784000000,47969168267966482601/448216905599932170240000,1874146871703306631451/2589697676799608094720000,-127815911224368644971/529710888436283473920000,-8261686062745723313/6808185700614144000000,1928729056033578813269/7806265724324177510400000,735728062520659571/636110270380376064000,-14057483433956196611/40368536389523865600000,8626564553140715171/48591756765167616000000,-3446402953971349231267/1443175175925478195200000,1264027174061004223/234949153589821440000,-59674386502451590187/7497013900911575040000,20179468448044261099/2186629054432542720000,-215558818248345298897/26239548653190512640000,259753585359205571/47600088259756032000,-1684287253491150881/629378944767885312000,43172313423257243/44955638911991808000,-1640642519592641/6643444416994344960,4578103566229/105451498682449920,-13920029/2958796259328,1/4194304,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[6506576816785801398187/82291051177250704588800000,-101572821057297674561/1994934573993956474880000,-1208619549879146855282461/1748045931839735463936000000,6480921340083711694037/34960918636794709278720000,22806828370668715384219/13446507167997965107200000,-1872350702119958055019/7264606469983316213760000,-473801109017925759933661/234187971729725325312000000,7968121440807194609573/49960100635674736066560000,4829751446365017466757/3148745838382861516800000,-10386920477855137261/22899969733693538304000,56240737484290087714933/86590510555528691712000000,-10039774593615437057/3044941030524085862400,3083910079261205205703/494802917460163952640000,-69033093286044483793/8396655569020964044800,1682543238186462109781/196796614898928844800000,-576830796974580137837/83966555690209640448000,47115613058706549433/11328821005821935616000,-242843983226089897/129472240066536407040,7455958186717394923/11958199950589820928000,-3608239994853353/24157979698161254400,108798482720747/4428962944662896640,-325333835/130187035410432,1/8388608,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[32729394708071881944913/2605883287279605645312000000,16261689498166991873658827/31270599447355267743744000000,-299585141850142292000239/3411338121529665572044800000,-451440331886548778437637/215144114687967441715200000,502689074619081097691/3145381793683734528000000,868219709611895617883/254007219230185881600000,-609270199000741620810163/5932761950486374907904000000,-1755270844679888312372747/576462699642400800768000000,-38725431474228964911409/832668343927912267776000000,281669914810065629951/146794677780086784000000,-683418666474764918561/1069018648833687552000000,4874246151776415181037/3806176288155107328000000,-13868123145820811058307/3298686116401093017600000,7304293910989642248817/1065729360683430051840000,-174079112571820301381/21420039716890214400000,7401120036431182575971/968844873348572774400000,-8853343069161575146093/1586034940815070986240000,144045360761130457/46477214382859223040,-1817790289778085173/1406847053010567168000,37130214140977399/92915306531389440000,-27750273842920897/310027406126402764800,2157831066851/156149334587473920,-990874363/748575453609984,1/16777216,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-1162842460088827304610539/4516864364617983118540800000,-2530182928784262000558347/451686436461798311854080000000,10689752177083411495658367697/4878213513787421768024064000000,38769833287612557645572797/500329591157684283899904000000,-575819837562030001422203/109681313370336342835200000,-1057471888696814075129549/4439481731656471019520000000,2385493455886279802405473979/391562288732100743921664000000,10982463364248037057753307/31641397069260666175488000000,-10490899543335806470194461/2498005031783736803328000000,-48308184106901842503899/126161870292107919360000000,57859356858458526530639/25189966707062892134400000,-524201187640186815637/563877968615571456000000,5321772692593331313437/2625484868155971993600000,-5521796787824939239021/1099562038800364339200000,3165213180818577838441/439824815520145735680000,-3089289653771453803501/399840741381950668800000,4354457810142028871713/658814513877029486592000,-13596674392885046879/3075698010630389760000,3897599274559740868553/1721980792884934213632000,-940893196173441949/1073687986584944640000,5650432223889647431/22321973241100999065600,-16125652167679129/303137908212482703360,144409995618451/18675460416661880832,-25128807667/35931621773279232,1/33554432,0,0,0,0,0,0,0,0,0,0,0,0,0],[73075015034600049415886537/1829330067670283163009024000000,-167366846352033465782698073/93333166717871589949440000000,-43237324394399993254086396737/109759804060216989780541440000000,118559998361316902228839770359/16725303475842588918939648000000,1118630592663475458398423407/1039146073942882743484416000000,-28413176752341045600721428689/2517186141849219068067840000000,-8547977261246193378329384309/5873434330981511158824960000000,674641477564432953454639513/68846116700149581348864000000,1023363530735440750655226121/854317720870037986738176000000,-62704907250779340335566283/11678984564183704535040000000,-4170266514131171985018967597/4946049962931798870589440000000,3794873972715980369021077/1425032402285272183603200000,-934514949005521524957703/701720501125323423744000000,20343265431457113942632323/7204330478219987150438400000,-23591013562671733375635329/4156344506665377202176000000,172219309926151748469551/23750540038087869726720000,-536099731805706012745751/75569900121188676403200000,2447448989551680099487787/440464560706356856750080000,-945047272827670747081/276253603136620462080000,145192430397245954699/89453547682334244864000,-3923932840898054107339/6696591972330299719680000,856110429354824443/5396521003343098675200,-6095165229703183/194271043343603466240,482485945560235/112052762499971284992,-25472027467/69099272640921600,1/67108864,0,0,0,0,0,0,0,0,0,0,0,0],[49436433238324628554645428469/51221241894767928564252672000000,43752540499403398468848952631/73173202706811326520360960000000,-5291687098444949440318589123077/658558824361301938683248640000000,-10584470103730263539352632055619/4214776475912332407572791296000000,79619914988962605960766853714527/4214776475912332407572791296000000,745842339464529705780610573973/173685843787596115696680960000000,-556769861076910055648410401269/25966762305391944070594560000000,-14151442793516625835390904189/3469844281687538899982745600000,130278151946501543074838726059/9021595132387601139955138560000,5184500491264817148974943191/2071073262715243604213760000000,-47606899638386179904536282943/7419074944397698305884160000000,-1539260153117259067049464099/1095740299480275442099814400000,11412901261083137047518113/3780698210144599670784000000,-137415800762357830216878097/75020300847580031483904000000,1686673681821549700085393/468876880297375196774400000,-1525081915728523051218983/249380670399922632130560000,795993260694850798974562771/113052570581298259899187200000,-1167886132484953798728152921/184995115496669879835033600000,617255024523219430903109/135605987439688569323520000,-97402228279886461449851/37570490026580382842880000,5575262228030659752737/4870248707149308887040000,-1252328168075124765883/3237912602005859205120000,247118729898093064439/2509981879999356783820800,-387042387597570209/21062785006987609374720,990641172124837/415010231481375129600,-232222818803/1197720725775974400,1/134217728,0,0,0,0,0,0,0,0,0,0,0],[-3560528973496278524907992449727/7427080074741349641816637440000000,62242967288528193204059203579/8755767845259474968248320000000,10942382892035076091818858890909/2634235297445207754732994560000000,-2239915160682075355418498838779/81053393767544853991784448000000,-214091926356571110172086788880811/21073882379561662037863956480000000,303515588589417380988144460978931/7024627459853887345954652160000000,37540632797009903540686142433119/3094765943851712606959042560000000,-1114474932040526336238833665027/30361137464765965374849024000000,-38589799195678852309398134809/4422350555091961343115264000000,15319835324322750235472306027/776652473518216351580160000000,297211522376917136076011831/70097864276515937373388800000,-6588258078925557656948558831/913116916233562868416512000000,-269139460542526014940284295777/132271507580118964082049024000000,611402950398770126434123399/181549128051143676191047680000,-108752440909961159080484163301/45387282012785919047761920000000,19414576489232054939386495927/4538728201278591904776192000000,-1071360705914340921190882931/169578855871947389848780800000,22426709321423665759717297/3391577117438947796975616000,-19196980929228310943417131429/3514907194436727716865638400000,7184454579202241765280511/1972450726395470099251200000,-3346054061710043302734553/1735756639228013687341056000,3124245972390822463297/3928667290433775835545600,-213714708973152683830999/847118884499782914539520000,866057053260536993/14301891054127389081600,-57514680496145319367/5378532599998621679616000,116080752096401/88083804232781660160,-235091155703/2309889971139379200,1/268435456,0,0,0,0,0,0,0,0,0,0],[-8373695909890277012553457995389/2025567293111277175040901120000000,-8782127038540990201628409247449991/1782499217937923914035992985600000000,85528633199970004650581382537283/2531759324775714546882969600000000,183970081978289747541009869849581/9366169946471849794606202880000000,-17166422822344550356440647061499/219519608120433979561082880000000,-612995367309754009383606584411177/19353565450617852891915878400000000,2315000923765676875351716530308482857/26553091798247694167708585164800000000,61066284781309461263682716777819929/2178715224471605675299165962240000000,-1361371727203576128693347001114679/23681687222517452992382238720000000,-195543072942705748074045663325379/12302175180528547009029734400000000,3411102840985810735568425062604957/135323926985814017099327078400000000,1405760553687312064479377006777/223061418108484643570319360000000,-425895682278692057560078098412391/55554033183649964914460590080000000,-3327678477714995650212678306671/1234534070747776998099124224000000,224540845249662212474175469807/60516376017047892063682560000000,-1621980322518658158893907673399/544647384153431028573143040000000,8904940062021212284244812954543/1851801106121665497148686336000000,-153531172425229519648523914837/24419355245560424138224435200000,2795518394836009964290917087889/463967749665648058626264268800000,-20234500580063875753491068803/4393633993045909646082048000000,5529025461456379757896873/1937846327686777641369600000,-537948355045376059357980859/381866460630163011215032320000,426025153620546663210371/781955893384414998036480000,-1222421871577499159423/7509780488685858324480000,83442801915681141422773/2258983691999421105438720000,-101771561207774182339/16451982070584019255296000,17237586510224693/23782627142851048243200,-6897956948587/129353838383805235200,1/536870912,0,0,0,0,0,0,0,0,0],[64415771762658729685163891432689/17665433425855384058540851200000000,-85880763485721482569702421929854373/2673748826906885871053989478400000000,-243835937724513462930940998995819051/8021246480720657613161968435200000000,136231588339062171493551276151496147/1106378824926987256987857715200000000,45392754712112656136013224123380307/632216471386849861135918694400000000,-12845216050009494027025043056494749/67737479077162485121705574400000000,-2182495740783038121441460129305865157/26553091798247694167708585164800000000,4203218939460576497840803805876406139/26553091798247694167708585164800000000,12232603119072317933557900588473493/217871522447160567529916596224000000,-149464193010876913340500084989377/1794067213827079772150169600000000,-124712963836031771479988299618789/4832997392350500610690252800000000,3564277445783423806692916376899/117400746372886654510694400000000,691509992155472611225309092669521/81194356191488410259596247040000000,-375132390836327892334139297846951/48609779035693719300153016320000000,-5639041893520681435473223574297/1683455551019695906498805760000000,2200871224276314756060827159683/544647384153431028573143040000000,-9847768847190085428629467709/2793063508479133479862272000000,511480384827732233195319991847/99203630685089223061536768000000,-1403634942393662455915019225243/231983874832824029313132134400000,6626105736494204997006073633/1240555480389433311834931200000,-19651352645130850023115199977/5179863233906756635380940800000,10985550890249690999098325567/5011997295770889522197299200000,-2966347934574063528518970191/2927642864831249752648581120000,282282387986012584814911/765997609845957549096960000,-612728382136898911003889/5893000935650663753318400000,65664602549675776487123/2936678799599247437070336000,-10601475256393157651/2978879593845390468710400,88172233059366947/221971186666609783603200,-6975593267347/250084087542023454720,1/1073741824,0,0,0,0,0,0,0,0],[55435586114986438751720921246668693/2762873787803782066755789127680000000,2916406291074221897297927405102561/82116372640607764213175156736000000,-41420944247026537137745649475075874333/256679887383061043621182989926400000000,-9454674662467091804403853688374408577/68447969968816278298982130647040000000,39162594690156678037863399327759920377/106212367192990776670834340659200000000,8298274665000803210756280238758407/38316149781021203705207193600000000,-3036512970595375246189112250377906983/7497343566564054823823600517120000000,-5721483519263733632654693362526819767/30898143183415498667879080919040000000,669080129268642327366337866744422296669/2549096812631778640100024175820800000000,23453659565314738888822551646035067/233017671066481890406327910400000000,-142403431396263883104454062488844011/1263023318534264159593719398400000000,-182202414331073935997021773070903/4758643586314339062833479680000000,129178386371773483793242152156062141/3725388107609468235440298393600000000,57354344492308806871234726753744297/5373590118854869333544187985920000000,-42951664642426502200898162401463641/5833173484283246316018361958400000000,-570900591525059067779913485021/144038977627353660449095680000000,482889712784499583418774251244213/111108066367299929828921180160000000,-767935291497408432391879210099/192394920116536675028434944000000,1920236244746947811866380893866363/361894844739205485728486129664000000,-19061381009827599395227348736249/3374310906659258608191012864000000,3271911973391153465805188769847/708605290398444307720112701440000,-7456849324352513275255866509/2437582698309061946061619200000,3050645643089403738625322703349/1844415004843687344168606105600000,-141147699125225726554633619/196541059457202780597387264000,6009998178744187713709229677/24397023873593747938738176000000,-3243102243373670363442749/49286916916351005936844800000,224346347530389325916491/16692700545090459115978752000,-5220784860675511556383/2562919679650252308715929600,399074696411670431/1839189832380481064140800,-218572480850557/15005045252521407283200,1/2147483648,0,0,0,0,0,0,0],[-32406606910966696951439431043143936229/1215664466633664109372547216179200000000,3588298932594701657756880983802314448751/21881960399405953968705849891225600000000,1583523381798434918848431708362619520199/7293986799801984656235283297075200000000,-1485722860190391707595796636689548748881/2391652597733933488799728564961280000000,-51066869602734095928365110209134671245941/101645235403692173273988464010854400000000,7587798761777903637971034470033396443/8032868107032915882668143411200000000,1072956792522677081896644177062420042861/1911822609473833980075018131865600000000,-2663094747070530002284777524476753891/3427357059000710776605074522112000000,-2845171249212507514695993473183969986397/7647290437895335920300072527462400000000,43845399261287874667662781221465462427/109052270059113524710161462067200000000,947818507981637507393787823096039913351/5751808192605038982789798140313600000000,-167065862575627509198397885471810189/1163908237170295401368329912320000000,-55143025732683950775149570210951237273/1044971364184455840041003699404800000000,1312449542303545906328874375285495289/34770289004355036864109451673600000000,33410862872409075749643192394002796819/2659927108833160320104373053030400000000,-1326508335988360540895115520835333/199638022991512173382446612480000000,-375689316600668528186130328705651/83331049775474947371690885120000000,38719883525115908385658286370631/8403131069795793012271349760000000,-1883806125956966518609717187734007/434273813687046582874183355596800000,128148776258120334255207152578337/24329065192551629292671336448000000,-299940383850591696431090193658489/58459936457871655386909297868800000,35770244301669566452853236444217/9170186111038691041083811430400000,-481805855562516060936422593700093/199196820523118233170209459404800000,14052427509351747030414622223/11445626403684161928906670080000,-110267074116345018260856332873/219573214862343731448643584000000,467513497024949298015991697/2870238102775735051616256000000,-3928066686672937114516580581/95148393107015616961078886400000,14581310952318458385803/1816985512018030537023160320,-118593041374269654704767/102196422226053810810047692800,1342324642296123053/11359701905879441866752000,-441657572729039/58144550353520453222400,1/4294967296,0,0,0,0,0,0],[-13569569581913444407037348987893505633633/123997775596633739155999816050278400000000,-388057516711184484509671632652010249837059/1487973307159604869871997792603340800000000,38748624887482936058442094549296615749979259/44639199214788146096159933778100224000000000,432039791922748493383124919986171251507/433038177353735638218050214297600000000,-265828755334960935146602046951150327047763/135526980538256231031984618681139200000000,-133798072457276959463603140630496439755669/87124487488879005663418683437875200000000,13558194639003283353052869685361341484729/6372742031579446600250060439552000000000,39280878007201013854565190490046178084977/30589161751581343681200290109849600000000,-1458582027428463638175067678740037277243/1073303921108117322147378600345600000000,-45670656305488963751373427663336482301/67228926926552403694945692549120000000,214638389599742095983184569924072755757527/373867532519327533881336879120384000000000,284330771800540236182422256049197745983/1137720301833963754837542489292800000000,-1927455970239711218133817149675816602347/11146361217967528960437372793651200000000,-114227786716356050946333482299431454367/1671954182695129344065605919047680000000,53648271223783659325630958612191635913/1364065184017005292361216950272000000000,62589633468592769740375598010203803/4470465729131361882528358072320000000,-794648016814279364143278275867010623/141033172242226044262843951349760000000,-1984602999565019444753652644845759/399989038922279747384116248576000000,709895191498505562472968332254217/147712181526206320705504542720000000,-98635870770121905936517248701124811/21713690684352329143709167779840000000,10801956662645976120429935193185827/2133274874252158652715286659072000000,-477082813287523279693847673473491/104989273638626646409143228825600000,2570521934722747604435137044709913/796787282092472932680837837619200000,-1198585500400487936545845841/638185437231660632333352960000,12613964750181215613957437711923/14052685751189998812713189376000000,-10774594648935238171865138123/31068863055352038191372697600000,405763732617482855313371586509/3805935724280624678443155456000000,-4889550884645927198977361791/190296786214031233922157772800000,109021333959656420935310273/22891998578636053621450683187200,-1345194126685941072702131/2043928444521076216200953856000,23281189352406813461/362821994205967628107776000,-446033437968239/112765188564403303219200,1/8589934592,0,0,0,0,0],[1323637868103206298655805876387086602905081/6509883218823271305689990342639616000000000,-26049372369542948254873579254216137714119/27775501733645957570943958795262361600000,-52611325461541817662972251648439228336721989/32324937362432795448943400322072576000000000,251155791298067766257926813046836710758184311/71422718743661033753855894044960358400000000,1834662236616864989139275095105106822964233/494274870198346254351943903425331200000000,-614379325328100197169760386946933745653351/116165983318505340884558244583833600000000,-15129738153431751809634759119165622629902559/3712260771265279371745665642135552000000000,3066623046220243699971396279853512541510037/713747107536898019228006769229824000000000,1030294732309378621117279264727135727871627/389316604111035283215276419579904000000000,-507875612485997710881350030715841291067/231736073875616240009093106892800000000,-15384548987556628352057464154835348056451241/13459231170695791219728127648333824000000000,531283977444209113194995712992763487364603/690216983112604677934775776837632000000000,19102865353861943453686066606221947225849711/53836924682783164878912510593335296000000000,-1818300619211985667291784594336311852957/9179356297149729732124895241830400000000,-19613997037679014503359460216850674410417/234073585577318108169184828666675200000000,36741885834815763426080363160214446757/933307757485319410562937913344000000000,978165151782004017803561226614425164499/65772743054783600642580860947660800000000,-11237841106854255192446221987976660563/2538597100360068796731191124295680000000,-806600965493376953600899340033543579/151995834790466304005964174458880000000,41225782965809933232851931370124917/8374426159254341818510422835200000000,-39146989276923366656303258984611383619/8511766748266113024333993769697280000000,843371267677230636889417245562660511/178341779487480463366997964698419200000,-9306395475510479579665749676562067001/2366458227814644610062088377729024000000,240546627693672113243645993286227/91936994087593030693942827417600000,-114268762991821527932511729604723211/79678728209247293268083783761920000000,78808104804485412460243308125713/121789943176979989710180974592000000,-6081353117229668061503178186907/25690066138894216579491299328000000,55388053822464035279425946711/801249626164342037566980096000000,-2452626840351365831956384535441/154520990405793361944792111513600000,13495377987303590082098311/4802517184329242018486157312000,-1811743202341680804297121/4873983213857950977094582272000,403471697710203146077/11610303814590964099448832000,-40934600117149/19899739158424112332800,1/17179869184,0,0,0,0],[29690898214716872776502046546318297474410557/44639199214788146096159933778100224000000000,11992254175712951581010965334130874100216207/5886487808543491792900211047661568000000000,-9019820687973925161249991389667943478860987659/1730627415711786587112662048012500992000000000,-15741576853852111229115204347483428179214639933/2045286945841202330224055147651137536000000000,3528152507138079092216289995154969933057352529/302496220561387907663389668896302694400000000,530186757915406596947526832913066653074600583/45374433084208186149508450334445404160000000,-1831695467598503279276806245919112391768052321/146369138981316729514543388175630336000000000,-2387503016589716819472125670848176712926393157/248383993422840510691346355691978752000000000,17640215477715553033256858732500881610448879/2234338771419854668887673364545536000000000,11647445237009884365130780507268463977439313/2335899624666211699291658517479424000000000,-531123261273258485531382404856690717222172987/161510774048349494636737531780005888000000000,-89028771213018126476779885482967806083077231/49695622784107536811303855932309504000000000,629721819389546466321554398960144371034840571/646043096193397978546950127120023552000000000,716824988536215758863729244646982645263645307/1507433891117928616609550296613388288000000000,-760545549932194147444838428160073871862789/3511103783659771622537772430000128000000000,-14720662774057196284496278619687704119199/150207648498813759252952831229952000000000,65938694311807564111580397241578096997801/1736400416646287056964134729018245120000000,6910027067545992112858801451239854851909/456947478064812383411614402373222400000000,-387175080508137483398674804697469344923/124028601189020504068866766358446080000000,-2446694509695066990554531827843169371/442169701208629248017350325698560000000,101043227618805016643514389367344419901/20428240195838671258401585047273472000000,-202933782920978401625137582888317956197/44942128430845076768483487104001638400000,794103065817909006764904874146133291753/184583741769542279584842893462863872000000,-189223351579430794000000592227042145239/56794997467551470641490121065496576000000,1992443914657890053177414516989974209/956144738510967519217005405143040000000,-4467368945454209993584921934034159847/4143293866880859249940356755619840000000,9083911618593668513800589459300513/19729970794670758333049317883904000000,-114978127485587063110269543497149/719321851889038064225756381184000000,164762419258480229971360662188237/3708503769739040686675010676326400000,-1641287716828851195937358874787/168568353169956394848864121651200000,2006308768222565210585649673/1216546210178944563882807735091200,-7126509036821051507942701/34076924654872396747417583616000,99210069767977798199/5284690012158645728024985600,-41309674280509/38662350364938275389440,1/34359738368,0,0,0],[-2136192980655303378459263060025577732210754261/1284616955181125537656158094280884224000000000,381227122867723601190442711129705245496813/63998851920843220209548292154982400000000,12730710282005622371008350050106955374880561829/969748120872983863468302009662177280000000000,-629017427853676128939953068591055416029144687323/28418723879056706062060555735784226816000000000,-1997747470267474342424305516672852847637633791303/67494469212759676897393819872487538688000000000,1521164248902193967223089128930493763842202953/46134292011455691659189213856792576000000000,1175372848374186224357577424693215304211437644311/36648580568014304197679902193205903360000000000,-68587016083950775673554128794295329370266395229/2588422668301180058783504127737462784000000000,-72016135697158180765108628905242544734395052797/3512859335551601508349041316215128064000000000,3939308971352819289549043199837836025400719/295061005220995162015788444313190400000000,49125984143621863236622085017425651508212209537/5652877091692232312285813612300206080000000000,-5183162236511429672480736969108845881214549/1120953897385884288976778705240064000000000,-1278050811202677547228972162398255352819588211/484532322145048483910212595340017664000000000,32169245167616018491521772713893836399543691/27463370485954165079931078278381568000000000,41073317140534834431228027698630081562641543707/67834525100306787747429763347602472960000000000,-5917799201645991055759325814970806798533/26088696844530810817618123318886400000000,-2914840720555706803152569025393299159525773/26528339698762718925840947248889856000000000,3466186218563021948447696232116896593107/97917316728174082159631657651404800000000,384622523071599746379214753475333448361519/26046006249694305854462020935273676800000000,-3744519531922120579551750051511733/2064372506497176635175233126400000000,-15424109466666518039029454554534211989/2735925026228393472107355140259840000000,17284742129188037001501857014690547581/3548062770856190271196064771368550400000,-22277193376164661371388559188815131030191/5168344769547183828375601016960188416000000,3426692063923959924159365922547001111/896763117908707431181422964192051200000,-218306743379533012445340414501977321369/78881940927154820335402945924300800000000,533807351492493850637139994415868049/327102147385330993416343954391040000000,-584218238732912920717754044073274343/731169505920151632342415898050560000000,672116692868984672816911232514913/2076839031017974561373612408832000000,-172278066716562472887755830538071/1612392943364800298554352467968000000,63341639873623144908929351233/2236643341485489538021195776000000,-37897572277484034745985994929309/6386867603439458960384740609228800000,290166548515841590820346979/301311754843082244924534423552000,-62252133539884066580110651/531032075871761515980590678016000,462454254730481573167/45707932912179163928356454400,-41674329717109/75176792376268868812800,1/68719476736,0,0],[-75471834509898488190497876700127449545926841657/16897653795074805149169464163233169408000000000,-809658148440066176996474503173578499145815361699/47072035571994100058400650169006686208000000000,70067158738145947265423943225897150905318845901/2023981873488689805950887906529771520000000000,230233900215334667174560240816569765856593431/3579231818677679772893386886873088000000000,-18433160872285611002594241421019862479536868347139/239980334978701073412955803991066804224000000000,-13754538385419917178199500338027758858314709996417/142405913064284153453841905665028653056000000000,25428831708692614360545800714497420160982978126911/311512934828121585680279168642250178560000000000,399958338375054563598874247844164697377895433961/5098932948593294497068508131228647424000000000,-10022038002707258791236054140687105044941615775973/196720122790889684467546313708047171584000000000,-110267565567909757538056274541555492701594350801/2751330388683771810734913478434226176000000000,1421924423557997440809898866727973107615983598249/67834525100306787747429763347602472960000000000,210838816967180000569331644656402386287177283/14908686835232261043391156779692851200000000,-1439533360383231752090976430172853895359530273/234924762252144719471618228043644928000000000,-398732612346446973165641337654115687816190016373/108535240160490860395887621356163956736000000000,363835908494991335230436345484027758738223720833/271338100401227150989719053390409891840000000000,361686673519993968851532823619150379636027539/491109683984121540252885164507529216000000000,-186668311534822395537725791951739322095360251/818588767847535326854520657965744128000000000,-786160086026897897073115744468854388213313/6628249132368707100036604517941248000000000,7031626754243846780494285301381537432934929/219944052775196360548790399008977715200000000,20223352190202267633504611073971600116063/1457119230752128998851022150225100800000000,-12173943309683604923435411761102359187153/20836804999755444683569616748218941440000000,-2332141407483709190970227612466558865559/414850416284723785555232188652322816000000,12679818194462437886884094789231938696091/2696527705850704606109009226240098304000000,-364366342957480604970802620537494711801/90871995948082353026384193704794521600000,56570444769837931227665100904187185258729/17038499240265441192447036319648972800000000,-2848868139194430698042947852734637080089/1262111054834477125366447134788812800000000,88892725873043876962096920516784234513/70654063835231494577930294148464640000000,-6256102525064754389463310145345022199/10708821071322836215230460537602048000000,11295028621417919616405067344141739/50221743840980112120489172795392000000,-162297156052041316143320801839/2293993170754348244124303360000000,15445197452147278902943408490280619/862227126464326959651939982245888000000,-229213616741004571806615420557/63778986834159053434954501324800000,3594979859849393219329786352837/6423363989744827297301224841281536000,-181904619739970205854518129/2777706243021521775898474315776000,330746557012989117164161/60791550773198288024714084352000,-1555077795250633/5412729051091358554521600,1/137438953472,0],[479081772973727104315227935178914956388985085993/32512753222401997004284320040831942656000000000,-570785660109361020733575504108990242074334774174273/13707376758564681937006269329214747023769600000000,-177313028304310420823548747419197620408443664814809423/1542079885338526717913205299536659040174080000000000,61409570133034071461104075288638394193730379466280893/400107213493239364647750564204106129342464000000000,21632816901349277148382496845020200703761236960928711/84233097577524076767947487200864448282624000000000,-1259065392807225016080035342874178509517549520954743/5553830609507081984699834320936117469184000000000,-53513950055613670992272546946886778091943054274128273/194384071332747869464494201232764111421440000000000,28060927963727642845471227413665365841915452712100641/155507257066198295571595360986211289137152000000000,542097214282345925698203304642362279163603478771/3119347215610015457030146150869290188800000000,-43668845890301139654138273162698009264078277373/485528892120665613659102378547216384000000000,-214204353699661808440388528031073570184155061555213/2950801841863345267013194705620707573760000000000,1411570406178012186926182959978971125789188247/45872882569945418595049713168285696000000000,436425650668254568756646231377668969009930382261/20156544601234016930664843966144734822400000000,-32543966496631132718965460772867437660324434508359/4232874366259143555439617232890394312704000000000,-3815917513261340249888227932105704888041805781979/783865623381322880636966154238961909760000000000,1123605712589196182582075515892595815189154019/766131107015229602794500856631745576960000000,25442151463700951675636067563526965061019948769/29600520043770234653423896733499260928000000000,-505339520583982140606277544199279186319382147/2292048549973098915192657842304083558400000000,-26939811620940644224808508257506165040252118493/217744612247444396943302495018887938048000000000,1665185661616108978126291792472877023222889877/59384894249303017348173407732423983104000000000,12688250591703182183691709196981217770561/1009395397912028462179843025865277440000000,139055735390214284497358410705343378803441/275045825996771869823118941076490027008000000,-10221095144008752272563301420091930911680469/1860604117036986178215216366105667829760000000,438782237436350480381775844008616946633/98627305435302739370008818770509824000000,-4840721230687742700073978350422746456593583 /1329002940740704413010868832932619878400000000,19266279542915799690295631946682424181911/6815399696106176476978814527859589120000000,-267273081248194672804448424069944239137211/147666993415633823667874314770291097600000000,211348908991315227248904581233914407249/221105658590253853620346567570489344000000,-4516955356325385416980600725204864899/10708821071322836215230460537602048000000,20789858233634992441152609978317789/134439745051239069368694093329203200000,-37978645823633608584896658462423349/817922965815962333266662433751040000000,73027021505144646607672997633664587/6492063069849050049144018689851392000000,-9730746636932174581594291842668599/4496354792821379108110857388897075200000,6429037671825529626127916131549/19854034150120375282567422236688384000,-5760784605778302752351051741/157982042571849051004225726709760000,6389419425006968382708619/2188495827835138368889707036672000,-1567859927923033/10540577625809487711436800,1/274877906944]])');
  
  n<-17
  Q<-Q[1:n,1:n]
  
  res<-z
  dn<-1
  for (i in 1:50) {
    dn=dn/(1+res)
    res<-log1p(res)
  }
  entier<-round(gam)
  gammaF<-gam-entier
  
  inter<-0:(n-1)
  
  VectQi<-Q%*%(gammaF^inter)
  
  Qprime<-t(t(cbind(Q, zeros(n,1))[1:n,2:(n+1)])*1:n)
  VectQprimei<-Qprime%*%(gammaF^inter)
  
  
  Phizg<-res+res^2*gammaF*horner(rev(VectQi),res)$y
  dPz=1+res*gammaF*horner(rev(VectQi*(inter+2)),res)$y
  dPg=res^2*horner(rev(VectQi),res)$y+res^2*gammaF*horner(rev(VectQprimei),res)$y
  
  
  
  dp<-0
  for (i in 1:(50+entier)) {
    dp<-dp+Phizg #pour deriv
    Phizg<-expm1(Phizg)
  }
  
  
  dPhiz<-dPz*dn*exp(dp)
  dPhig<-dPg*exp(dp)
  
  
  
  result<-list("z"=Phizg,"dz"=dPhiz,"dg"=dPhig)
  
  
  return(result)
}



PhiContExp<- function(z,gam){
  
  #seuil à 700 plutot que zero?
  Phiz<-z*0
  dPhiz<-z*0
  
  Phin<-PhiCont(exp(z[z<0]),gam)
  Phiz[z<0]<-Phin$z
  dPhiz[z<0]<-Phin$dz*exp(z[z<0])
  
  Phip<-PhiCont(z[z>=0]+log(1+exp(-z[z>=0])),gam+1)
  Phiz[z>=0]<-Phip$z
  dPhiz[z>=0]<-Phip$dz/(1+exp(-z[z>=0]))
  
  
  result<-list("z"=Phiz,"dz"=dPhiz)
  
  return(result)
  
}


LogPhiCont<- function(z,gam){
  
  
  if(gam<0){return(log(PhiCont(z,gam)$z))}
  else{
    
    phi<- PhiCont(z,gam-1)$z
    return(phi+log(1-exp(-phi)))
  }
  
  
}



VerifParam<- function(UFP=NULL,UF=NULL,NUFP=0,NUF=0,NUM=1,PRO=0.5,BOO=TRUE)
{
  
  if(anyNA(c(NUM,PRO))){warning("Vectors contain NA/NaN")}
  AllNum<-c(UFP,UF,NUM[!is.na(NUM)],PRO[!is.na(NUM)])
  flag<-is.vector(AllNum,mode = "numeric")&&all(is.finite(c(0,UFP,UF)))&&(length(UFP)==NUFP)&&(length(UF)==NUF)&&all(UFP>0)&&(!any(PRO[is.finite(PRO)]>1|PRO[is.finite(PRO)]<0))&&is.logical(BOO)
  
  if(!is.logical(flag)){warning("********************Revoir code**************************")}
  
  if(!isTRUE(flag)){warning("incorrect arguments")}
  return(isTRUE(flag))
}









pTetration<- function(x,tetration=0,shapein=1, shapeout=1, scalein=1, scaleout=1, location=0,lower.tail = TRUE){
  
  flag<-VerifParam(c(shapein,shapeout,scalein,scaleout),c(tetration,location),4,2,x,BOO=lower.tail) 
  stopifnot(flag)
  
  res<-x
  cond<-((x>=location)&is.finite(x))%in%T
  res[cond]<- exp(-(scalein/scaleout*PhiCont(((x[cond]-location)/scalein)^shapein,tetration)$z)^(shapeout/shapein))
  res[(x==Inf)%in%T]<-0
  res[(x<location)%in%T]<-1
  return((1-2*res)*lower.tail +res)
  
  
}

dTetration<- function(x,tetration,shapein=1, shapeout=1, scalein=1, scaleout=1, location=0){
  
  flag<-VerifParam(c(shapein,shapeout,scalein,scaleout),c(tetration,location),4,2,x) 
  stopifnot(flag)
  
  res<-x
  cond<-((x>=location)&is.finite(x))%in%T
  phi<-PhiCont(((x[cond]-location)/scalein)^shapein,tetration)
  res[cond]<- exp(-(scalein/scaleout*phi$z)^(shapeout/shapein))*shapeout/scaleout*((x[cond]-location)/scalein)^(shapein-1)*phi$dz*((scalein/scaleout*phi$z))^(shapeout/shapein-1)
  res[(x==Inf)%in%T]<-0
  res[(x<location)%in%T]<-0
  return(res)
  
  
}

qTetration<- function(p,tetration=0,shapein=1, shapeout=1, scalein=1, scaleout=1, location=0){
  
  
  flag<-VerifParam(c(shapein,shapeout,scalein,scaleout),c(tetration,location),4,2,PRO=p) 
  stopifnot(flag)
  
  res<-p
  cond<-((p>0)&(p<1))%in%T
  res[cond]<-location+scalein*(PhiCont(scaleout/scalein*(log1p(p[cond]/(1-p[cond])))^(shapein/shapeout),-tetration)$z)^(1/shapein)
  res[(p==1)%in%T]<-Inf
  res[(p==0)%in%T]<-location
  return(res)
  
}

rTetration<- function(n,tetration=0,shapein=1, shapeout=1, scalein=1, scaleout=1, location=0){
  
  return(qTetration(runif(n),tetration,shapein,shapeout,scalein,scaleout,location))
  
  
}


hTetration<- function(x,tetration=0,shapein=1, shapeout=1, scalein=1, scaleout=1, location=0){
  
  
  
  flag<-VerifParam(c(shapein,shapeout,scalein,scaleout),c(tetration,location),4,2,x) 
  stopifnot(flag)
  
  hres<-x
  Hres<-x
  cond<-((x>=location)&is.finite(x))%in%T
  phi<-PhiCont(((x[cond]-location)/scalein)^shapein,tetration)
  common<-((scalein/scaleout*phi$z))
  Hres[cond]<- (common)^(shapeout/shapein)
  hres[cond]<- shapeout/scaleout*((x[cond]-location)/scalein)^(shapein-1)*phi$dz*(common)^(shapeout/shapein-1)
  hres[(x==Inf)%in%T]<-ifelse(tetration<0,0,ifelse((tetration>0)|(shapeout>1),Inf, ifelse(shapeout<1,0,1/scaleout)))
  hres[(x<location)%in%T]<-0
  Hres[(x==Inf)%in%T]<-Inf
  Hres[(x<location)%in%T]<-0
  return(list("h"=hres,"H"=Hres))
  
  
}

pITetration<- function(x,tetration=0,shape=1, scale=1,lower.tail = TRUE){
  
  return(pTetration(x,tetration,shapein=shape, shapeout=shape, scalein=scale, scaleout=scale, location=0,lower.tail))
  
}

dITetration<- function(x,tetration=0, shape=1, scale=1){
  
  return(dTetration(x,tetration,shapein=shape, shapeout=shape, scalein=scale, scaleout=scale, location=0)) 
  
  
}

qITetration<- function(p,tetration=0,shape=1,scale=1){
  
  return(qTetration(p,tetration,shapein=shape, shapeout=shape, scalein=scale, scaleout=scale, location=0))
  
}

rITetration<- function(n,tetration=0,shape=1, scale=1){
  
  return(qTetration(runif(n),tetration,shapein=shape,shapeout=shape,scalein=scale,scaleout=scale,location=0))
  
  
}


hITetration<- function(x,tetration=0,shape=1, scale=1){
  
  return(hTetration(x,tetration,shape,shape,scale,scale))
  
  
}

pOTetration<- function(x,tetration=0,shape=1, scale=1,lower.tail = TRUE){
  
  return(pTetration(x,tetration,shapein=1, shapeout=shape, scalein=1, scaleout=scale, location=0,lower.tail))
  
}

dOTetration<- function(x,tetration=0, shape=1, scale=1){
  
  return(dTetration(x,tetration,shapein=1, shapeout=shape, scalein=1, scaleout=scale, location=0)) 
  
  
}

qOTetration<- function(p,tetration=0,shape=1,scale=1){
  
  return(qTetration(p,tetration,shapein=1, shapeout=shape, scalein=1, scaleout=scale, location=0))
  
}

rOTetration<- function(n,tetration=0,shape=1, scale=1){
  
  return(qTetration(runif(n),tetration,shapein=1,shapeout=shape,scalein=1,scaleout=scale,location=0))
  
  
}


hOTetration<- function(x,tetration=0,shape=1, scale=1){
  
  return(hTetration(x,tetration,shapein=1, shapeout=shape, scalein=1, scaleout=scale, location=0))
}



eITetration<- function(X, method="mle", plot.pos.con = 0.375){
  
  ResiduITetration<- function(x){
    if (max(abs(x))<1.2){
      ORD<-LogPhiCont(-log(1-Femp),-x);
      mylm<-lm(ORD~ABS)
      return(-summary(mylm)$r.squared) 
    }
    else{return(Inf)}
    
  }
  
  
  logLITetration<- function(x){
    if((abs(x[1]<2))&(x[2]>0)&(x[3]>0)){
      Like<- log(dITetration(X,x[1],x[2],x[3]))
      return(-sum(Like[is.finite(Like)]))}
    else{return(Inf)}
  }
  
  stopifnot(all(is.numeric(X)))
  method <- match.arg(method, c("mle", "lse"))
  data.name <- deparse(substitute(X))
  if ((bad.obs <- sum(!(X.ok <- is.finite(X)))) > 0) {
    X <- X[X.ok]
    warning(paste(sep = "",bad.obs, " observation",ifelse(bad.obs==1,character(1),"s")," with NA/NaN/Inf in ",data.name," removed."))
  }
  n <- length(X)
  if (n < 3  || length(unique(X)) < 3) 
    stop(paste("'X' must contain at least 3 non-missing distinct values"))  
  if (!is.vector(plot.pos.con, mode = "numeric") || length(plot.pos.con) != 
      1 || plot.pos.con < 0 || plot.pos.con > 1) 
    stop("'plot.pos.con' must be a numeric scalar between 0 and 1")
  
  
  ABS<-log(sort(X))
  n<-length(X)
  Femp <- ppoints(n, a = plot.pos.con)
  
  estim<-optimize(f=ResiduITetration,interval=c(-2,2))
  ORD<-LogPhiCont(-log(1-Femp),-estim$minimum)
  mylm<-lm(ORD~ABS)
  reslse<-as.vector(c(estim$minimum,(mylm$coefficients[2]),(exp(-mylm$coefficients[1]/mylm$coefficients[2]))))
  
  switch(method, mle = {
    estim<-fminsearch(logLITetration,reslse)
    dist.params<-c(tetration=estim$xmin[1], shape=estim$xmin[2], scale=estim$xmin[3])
    
  }, lse = {
    
    dist.params<-c(tetration=reslse[1], shape=reslse[2], scale=reslse[3])
    
  }) 
  
  ret.list <- list(distribution = "I Tetration", sample.size = n, 
                   parameters = dist.params, n.param.est = 3, method = method, 
                   data.name = data.name, bad.obs = bad.obs)
  oldClass(ret.list) <- "estimate"
  ret.list
  
  
}




eOTetration<- function(X, method="mle", plot.pos.con = 0.375){
  
  ResiduOTetration<- function(x){
    if (max(abs(x))<1.2){
      ABS<-LogPhiCont(sort(Xsim),x)
      mylm<-lm(ORD~ABS)
      return(-summary(mylm)$r.squared) 
    }
    else{return(Inf)}
    
  }
  
  
  logLOTetration<- function(x){
    if((abs(x[1]<2))&(x[2]>0)&(x[3]>0)){
      Like<- log(dOTetration(X,x[1],x[2],x[3]))
      return(-sum(Like[is.finite(Like)]))}
    else{return(Inf)}
  }
  
  stopifnot(all(is.numeric(X)))
  method <- match.arg(method, c("mle", "lse"))
  data.name <- deparse(substitute(X))
  if ((bad.obs <- sum(!(X.ok <- is.finite(X)))) > 0) {
    X <- X[X.ok]
    warning(paste(sep = "",bad.obs, " observation",ifelse(bad.obs==1,character(1),"s")," with NA/NaN/Inf in ",data.name," removed."))
  }
  n <- length(X)
  if (n < 3  || length(unique(X)) < 3) 
    stop(paste("'X' must contain at least 3 non-missing distinct values"))  
  if (!is.vector(plot.pos.con, mode = "numeric") || length(plot.pos.con) != 
      1 || plot.pos.con < 0 || plot.pos.con > 1) 
    stop("'plot.pos.con' must be a numeric scalar between 0 and 1")
  
  n<-length(X)
  Femp <- ppoints(n, a = plot.pos.con)
  ORD<-log(-log(1-Femp))
  
  
  estim<-optimize(f=ResiduOTetration,interval=c(-2,2))
  ABS<-LogPhiCont(sort(Xsim),estim$minimum)
  mylm<-lm(ORD~ABS)
  reslse<-as.vector(c(estim$minimum,(mylm$coefficients[2]),(exp(-mylm$coefficients[1]/mylm$coefficients[2]))))
  
  switch(method, mle = {
    estim<-fminsearch(logLOTetration,reslse)
    dist.params<-c(tetration=estim$xmin[1], shape=estim$xmin[2], scale=estim$xmin[3])
    
  }, lse = {
    
    dist.params<-c(tetration=reslse[1], shape=reslse[2], scale=reslse[3])
    
  }) 
  
  ret.list <- list(distribution = "O Tetration", sample.size = n, 
                   parameters = dist.params, n.param.est = 3, method = method, 
                   data.name = data.name, bad.obs = bad.obs)
  oldClass(ret.list) <- "estimate"
  ret.list
  
  
}



eTetration<- function(X){
  
  locationstart<-min(X)
  paramstart<-eITetration(sort(X)[2:length(X)]-locationstart,method="mle")$parameters
  logLTetration<- function(x){
    gam<-x[1]
    shapein<-x[2]
    shapeout<-x[3]
    scalein<-x[4]
    scaleout<-x[5]
    location<-x[6]
    if (abs(gam)<2 & shapein>0&shapeout>0&scalein>0&scaleout>0&location<min(X)){
      intens<-hTetration(X,gam,shapein,shapeout,scalein,scaleout,location)
      return(-sum(log(intens$h))+sum(intens$H))
    }
    return(10^100)
  }

  estim<-nlm(logLTetration,x<-c(paramstart[1],1,paramstart[2],1,paramstart[3],locationstart-0.1), hessian=TRUE)

  return(estim$estimate)
  
  
}


