forked from Psychedelic-Geometry/OEIS-Mathematica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OEIS.m
533 lines (430 loc) · 16.7 KB
/
OEIS.m
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
(* ::Package:: *)
(* ::Section:: *)
(*(*OEIS Package: Title and comments*)*)
(* :Title: Package OEIS *)
(* :Context: Utilities`OEIS` *)
(* :Author: Enrique P\[EAcute]rez Herrero *)
(* :Summary:
This package provides various utilities for
searching and working with the information in
The On-Line Encyclopedia of Integer Sequences.
http://oeis.org/
*)
(* :Package Version: 2.02 *)
(* :Mathematica Version: 9.0.0.0 *)
(* :Links:
The OEIS Foundation Inc: http://oeisf.org/
OEIS Wiki: http://oeis.org/wiki/
Psychedelic Geometry Blogspot: http://psychedelic-geometry.blogspot.com/
OEIS Page: http://oeis.org/wiki/User:Enrique_P\[EAcute]rez _Herrero/
*)
(* :History:
V. 1.0 26 August 2010, by E.P\[EAcute]rez Herrero.
V. 2.0 21 November 2010, by EPH.
*)
(* :Keywords:
packages, sequence, OEIS
*)
(* :Licence:
OEIS.m: Wolfram Mathematica Package for working with OEIS data.
Copyright (C) 2015 Enrique P\[EAcute]rez Herrero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: [email protected]
*)
(* :Limitations: *)
(* :Discussion: *)
(* :Changes:
29/Aug/2010 - Added OEISbFile function and exporting to Html
28/Nov/2010 - Code rebuilt due to changes in OEIS, old links are no longer valid.
04/Jun/2011 - Fixed Unicode LF, instead of Window's CRLF for bFiles.
*)
(* ::Section:: *)
(*(*Begin Package and Help*)*)
BeginPackage["OEIS`"];
(*Help on Package functions *)
(*
Old links are no more valid
OEISServerURL::usage="http://oeis.org/classic/";
OEISWikiURL::usage="http://oeis.org/wiki/";
*)
OEISServerURL::usage="http://oeis.org/";
OEISTotalNumberOfSequences::usage="OEISTotalNumberOfSequences[]: Reads the actual number of Sequences that OEIS contais from http://oeis.org/
or returns a default value if it cannot connect.";
OEISValidateIDQ::usage="OEISValidateIDQ[ID]: Checks that the Sequence ID is valid.";
OEISImport::usage="OEISImport[ID,element]: Retrieves Data, Description, Image, Offset or bFile from OEIS Server";
OEISURL::usage="OEISURL[ID]: Gives the OEIS sequence URL when the ID is entered";
OEISFunction::usage="OEISFunction[ID]: Creates a function named with the OEIS ID and preloads data from the sequence and its bFile, if found";
OEISExport::usage="OEISExport[ID,filename]: Export required data to file";
(*Help on other Functions *)
OEISbFile::usage="OEISGeneratebFile[ID,Vmax,filena,a]: Save the bFile up to Vmax values.";
(*Help on Options *)
bFile::usage="bFile";
URL::usage="URL";
URLType::usage="URLType";
AddHelp::usage="AddHelp";
Output::usage="Output";
(* Error Messages *)
OEIS::conopen="Cannot connect to OEIS Server: `1`";
OEIS::bFile ="Cannot open bFile from OEIS Server: `1`";
OEIS::ID="`1` is not a valid OEIS ID";
Unprotect["`*"];
Begin["`Private`"];
(* ::Section:: *)
(*(* URLs *)*)
(*
Old OEIS links are no longer valid
OEISServerURL="http://oeis.org/classic/";
OEISWikiURL="http://oeis.org/wiki/"
*)
OEISServerURL="http://oeis.org/";
(* ::Section:: *)
(*(* Importing Data *) *)
(*OEISTotalNumberOfSequences[]: *)
Options[OEISTotalNumberOfSequences]={URL->True};
OEISTotalNumberOfSequences[OptionsPattern[OEISTotalNumberOfSequences]]:=OEISTotalNumberOfSequences[OptionsPattern[OEISTotalNumberOfSequences]]=
Module[{dataloaded,first,last, defaultvalue=20000,urlQ},
dataloaded=Quiet[Import[OEISServerURL,"Plaintext"]];
urlQ=OptionValue[URL];
If[Head[Element[urlQ,Booleans]]===Symbol,
If[!urlQ,Return[ defaultvalue]];
If[dataloaded===$Failed,
(*No internet connection to OEIS Server *)
Message[OEIS::conopen,OEISServerURL];
Return[defaultvalue];,
(*Read OEIS Data from: http://oeis.org/*)
first=2+Last[Flatten[StringPosition[dataloaded,"Contains"],1]];
last=-2+First[Flatten[StringPosition[dataloaded,"sequences."],1]];
Return[ToExpression[StringTake[dataloaded,{first,last}]]];
],
(* URL option is not True or False*)
Message[General::opttf,"URL",1];
Return[$Failed];
];
]
(*Execute Function and connect to OEIS Server *)
OEISTotalNumberOfSequences[];
(* OEISValidateIDQ *)
OEISValidateIDQ[ID_]:=Module[{result,IDNumber},
Which[
(*ID is a String*)
StringQ[ID],
IDNumber=ToExpression[StringDrop[ID,1]];
result=((StringLength[ID]==7)&&
(StringTake[ID,1]=="A")&&
NumberQ[IDNumber](*&&
(IDNumber<=OEISTotalNumberOfSequences[])*)),
(*ID is a List*)
ListQ[ID],
result=And@@OEISValidateIDQ/@ID,
(*Error *)
True,
Message[General::string,"ID",1];
result=$Failed;
];
If[result===False ,Message[OEIS::ID,ID]];
Return[result];
];
(* OEISImport *)
OEISImport[ID_?OEISValidateIDQ,element_:"Data"]:=Module[{result,myurl,dataloaded},
Which[
(* Data *)
element=="Data",
myurl=OEISServerURL<>ID<>"/list";
dataloaded=ToExpression[Drop[Import[myurl,"Data"][[2]][[2]][[1]],1]];
If[dataloaded===$Failed, result=$Failed,result=dataloaded];,
(* Description *)
element=="Description",
myurl=OEISServerURL<>ID<>"/internal";
dataloaded=Quiet[Import[myurl,"Plaintext"]];
If[dataloaded===$Failed, result=$Failed,
result=Module[{first,last},
first=2+Flatten[StringPosition[dataloaded,"%N"],1][[2]];
last=-3+Select[StringPosition[dataloaded,"%"][[All,1]],#>first&][[1]];
Return[StringReplace[StringTake[dataloaded,{first,last}]," "~~_->""]]
]
];,
(* Author *)
element=="Author",
myurl=OEISServerURL<>ID<>"/internal";
dataloaded=Quiet[Import[myurl,"Plaintext"]];
If[dataloaded===$Failed, result=$Failed,
result=Module[{first,last,authorfound,resultstring,anotherentryfound},
authorfound=StringPosition[dataloaded,"%A"];
If[authorfound!={},
first=2+Flatten[authorfound,1][[2]];,
(*No Author Found*)
Return[{}];
];
last=-2+Select[StringPosition[dataloaded,"Lookup"][[All,1]],#>first&][[1]];
anotherentryfound=Select[StringPosition[dataloaded,"%"][[All,1]],#>first&];
If[anotherentryfound!={},last=Min[last,-2+anotherentryfound[[1]]] ];
resultstring=StringReplace[StringTake[dataloaded,{first,last}]," "~~_->""];
(* Remove emails *)
resultstring=StringReplace[resultstring,"(AT)"->""];
resultstring=StringReplace[resultstring,"("~~Shortest[___]~~")"->""];
(* Remove ending dot*)
resultstring=StringReplace[resultstring," ."->""];
(*Remove Date*)
resultstring=StringReplace[resultstring,DatePattern[{" , ","MonthName"," ","Day"," ","Year"}]->""];
(* Remove Spaces and newlines *)
resultstring=StringTrim[resultstring];
Return[{resultstring}]
]
];,
(* Date *)
element=="Date",
myurl=OEISServerURL<>ID<>"/internal";
dataloaded=Quiet[Import[myurl,"Plaintext"]];
If[dataloaded===$Failed, result=$Failed,
result=Module[{first,last,authorfound,resultstring,anotherentryfound},
authorfound=StringPosition[dataloaded,"%A"];
If[authorfound!={},
first=10+Flatten[authorfound,1][[2]];,
(*No Author Entry Found*)
Return[{""}];
];
last=-2+Select[StringPosition[dataloaded,"page"][[All,1]],#>first&][[1]];
anotherentryfound=Select[StringPosition[dataloaded,"%"][[All,1]],#>first&];
If[anotherentryfound!={},last=Min[last,-2+anotherentryfound[[1]]] ];
resultstring=StringReplace[StringTake[dataloaded,{first,last}]," "~~_->""];
resultstring=StringCases[resultstring,DatePattern[{"MonthName"," ","Day"," ","Year"}]];
If[Length[resultstring]==0,Return[{""}],resultstring=Flatten[StringSplit[resultstring],1]];
Return[resultstring]
]
];,
(* Image *)
element=="Image",
myurl=OEISServerURL<>ID<>"/graph?png=1";
dataloaded=Quiet[Import[myurl,"Image"]];
If[dataloaded===$Failed, result=$Failed,result=dataloaded];,
(* bFile *)
element=="bFile",
myurl=OEISServerURL<>ID<>"/b"<>StringDrop[ID,1]<>".txt";
result=Module[{dataloaded2=Quiet[Import[myurl,"Data"]]},
If[dataloaded2===$Failed,
(* No bFile Found *)
Message[OEIS::bFile,myurl];
Return[$Failed];,
Select[dataloaded2,#!={}&]]];,
(* Offset or MinData*)
(element=="Offset")||(element=="MinData"),
myurl=OEISServerURL<>ID<>"/list";
dataloaded=ToExpression[Drop[Import[myurl,"Data"][[2]][[2]][[1]],1]];
If[dataloaded===$Failed, result=$Failed,result=dataloaded[[1]][[1]]];,
(* MaxData *)
element=="MaxData",
Module[{mymaxdata},
mymaxdata=Quiet[OEISImport[ID,"bFile"]];
If[mymaxdata===$Failed,
mymaxdata=First[Last[OEISImport[ID,"Data"]]],
mymaxdata=First[Last[mymaxdata]];
];
Return[mymaxdata]
],
(* Erroneous Input *)
True,
Message[General::optx,element,2];
Return[$Failed];
];
If[result===$Failed&&(element!="bFile"),Message[OEIS::conopen,myurl]];
Return[result];
]
(* Attributes *)
SetAttributes[OEISImport,{Listable}];
(* OEISURL: *)
Options[OEISURL]={URL->True,bFile->False};
OEISURL[ID_?OEISValidateIDQ,OptionsPattern[OEISURL]]:=
Module[{urlQ,urltype,bfileQ,url="",result="",myid=""},
urlQ=OptionValue[URL];
bfileQ=OptionValue[bFile];
If[!(Head[Element[bfileQ,Booleans]]===Symbol),
(* bFile option is not True or False*)
Message[General::opttf,"bFile",bfileQ];
Return[$Failed];
];
If[!(Head[Element[urlQ,Booleans]]===Symbol),
(* URL option is not True or False*)
Message[General::opttf,"URL",urlQ];
Return[$Failed];
];
If[bfileQ,myid=("b"<>StringDrop[ID,1]<>".txt"),myid=ID];
If[urlQ,url=OEISServerURL];
result=url<>myid;
Return[result];
]
(* OEISFunction *)
Options[OEISFunction]={AddHelp->True,bFile->True,Output->True};
OEISFunction[ID_?OEISValidateIDQ,OptionsPattern[OEISFunction]]:=Module[{descriptionloaded,dataloaded,bfileloaded,bfileQ,addhelpQ,outputQ},
addhelpQ=OptionValue[AddHelp];
bfileQ=OptionValue[bFile];
outputQ=OptionValue[Output];
(* Invalid Values for Valid Options *)
If[!(Head[Element[addhelpQ,Booleans]]===Symbol),
(* bFile option is not True or False*)
Message[General::opttf,"AddHelp",addhelpQ];
Return[$Failed];
];
If[!(Head[Element[bfileQ,Booleans]]===Symbol),
(* bFile option is not True or False*)
Message[General::opttf,"bFile",bfileQ];
Return[$Failed];
];
If[!(Head[Element[outputQ,Booleans]]===Symbol),
(* bFile option is not True or False*)
Message[General::opttf,"Output",outputQ];
Return[$Failed];
];
descriptionloaded=Quiet[OEISImport[ID,"Description"]];
If[descriptionloaded===$Failed,
Return[$Failed];
Message[OEIS::conopen,OEISURL[ID]],
(*Adding Help *)
If[addhelpQ,
ToExpression[StringJoin[ID,"::usage=\"",ID,"[n]: ",descriptionloaded,"\""]]];
(*Loading data from bFile if exists*)
If[bfileQ,bfileloaded=Quiet[OEISImport[ID,"bFile"]],bfileloaded={}];
If[bfileloaded===$Failed||!bfileQ,dataloaded=OEISImport[ID,"Data"],dataloaded=bfileloaded];
(ToExpression[StringJoin["("<>ID<>"[#[[1]]]=#[[2]])&"]]/@dataloaded);
If[outputQ,
(* Prints information about function created *)
Print[ID,"(n): ",descriptionloaded];
Print["Link-1: ",Hyperlink[OEISURL[ID]]];
(* Old code, wiki link removed
Print["Link-2: ",Hyperlink[OEISURL[ID,URLType->"Wiki"]]];*)
If[bfileQ&&!bfileloaded===$Failed,
Print["Link-2: ",Hyperlink[OEISURL[ID,bFile->True]]]];
Print["Help Added: ",addhelpQ||descriptionloaded===$Failed];
Print["Loaded data from bFile: ",bfileQ&&!bfileloaded===$Failed];
Print["Loaded data from Sequence: ",!bfileQ||bfileloaded===$Failed];
Print["Data available: Table of n, ",ID,"(n) for n=",First[First[dataloaded]],"..",First[Last[dataloaded]]];
]
]
]
(* ::Section:: *)
(*(* Exporting Data *)*)
(* OEISExport *)
OEISExport[ID_?OEISValidateIDQ,filename_]:=Module[{myfileextension=ToLowerCase[FileExtension[filename]]},
Which[
(*AddHelp *)
MemberQ[{"gp","m"},myfileextension],
Module[{myhelptype,myhelpstring,myID},
If[ListQ[ID],myID=ID,myID={ID}];
Which[
(*Help for PARI/GP *)
myfileextension==="gp",myhelpstring=StringJoin["addhelp(",#,",\"",#,": ",OEISImport[#,"Description"],"\");"]&/@myID,
(*Help for Mathematica *)
myfileextension==="m",myhelpstring=StringJoin[#,"::usage=\"",#,"[n]: ",OEISImport[#,"Description"],"\";"]&/@myID,
(*Wrong HelpType option value *)
True,
Message[General::optx,myhelptype,2];
Return[$Failed];
];
Return[Export[filename,myhelpstring,"Text"]];
],
(*Data *)
MemberQ[{"txt","xls","csv","tsv","dat"},myfileextension],
Module[{myfilename,myID,myresult},
If[ListQ[ID],myID=ID,myID={ID}];
If[Length[myID]>1,myfilename=Function[#<>"_"<>filename],myfilename=Function[filename]];
Module[{dataloaded},
dataloaded=Quiet[OEISImport[#,"bFile"]];
If[dataloaded===$Failed,dataloaded=OEISImport[#,"Data"]];
Export[myfilename[#],dataloaded]]&/@myID
],
(*Image *)
(MemberQ[{"jpg","jpeg","jp2", "j2k","bmp","pgn","gif","tiff", "tif"},myfileextension]),
If[ListQ[ID],
Return[Export[#<>"_"<>filename,OEISImport[#,"Image"],"Image"]&/@ID];,
Export[filename,OEISImport[ID,"Image"],"Image"];
],
(*BibTeX *)
(MemberQ[{"bib"},myfileextension]),
Module[{mybibtex,myID},
If[ListQ[ID],myID=ID,myID={ID}];
mybibtex=(Module[{mydate,mymonth={},myyear={},myauthor,mydescription,mycitation={}},
(*Reading Online Data*)
mydate=OEISImport[#,"Date"];
If[Length[mydate]!=1,
mymonth=mydate[[1]];
myyear=mydate[[3]];
];
myauthor=Flatten[OEISImport[#,"Author"]];
mydescription=OEISImport[#,"Description"];
(*Function Output*)
mycitation="@MISC{oeis"<>#<>","<>"\n"<>
"AUTHOR={"<>myauthor<>"},"<>"\n"<>
"TITLE={The {O}n-{L}ine {E}ncyclopedia of {I}nteger {S}equences},"<>"\n"<>
"HOWPUBLISHED={\\href{"<>OEISServerURL<>#<>"}{"<>#<>"}},"<>"\n"<>
"MONTH={"<>mymonth<>"},"<>"\n"<>
"YEAR={"<>myyear<>"},"<>"\n"<>
"NOTE={"<>mydescription<>"}"<>"\n"<>
"}"<>"\n";mycitation]&/@myID);
Return[Export[filename,mybibtex,"Text"]];
],
(*HTML *)
(MemberQ[{"htm","html"},myfileextension]),
Module[{myhtmlcode,myID},
If[ListQ[ID],myID=ID,myID={ID}];
myID=({#,Part[myID,#]}&/@Range[Length[myID]]);
myhtmlcode=(Module[{separator,myauthor,mydescription,mycitation={}},
(*Reading Online Data*)
myauthor=Flatten[OEISImport[#[[2]],"Author"]];
If[Length[myauthor]==1,separator=", ",separator=""];
mydescription=OEISImport[#[[2]],"Description"];
(*Function Output*)
mycitation="<a name=\"oeis"<>ToString[#[[2]]]<>"\">["<>ToString[#[[1]]]<>"]-</a> "<>
myauthor<>separator<>
"The On-Line Encyclopedia of Integer Sequences. "<>
"<a href=\""<>OEISServerURL<>ToString[#[[2]]]<>"\">"<>ToString[#[[2]]]<>"</a>: "<>
mydescription<>"<br/>";
mycitation])&/@myID;
Return[Export[filename,myhtmlcode,"Text"]];
],
(*Wiki *)
(MemberQ[{"wiki"},myfileextension]),
Module[{mywikicode,myID},
If[ListQ[ID],myID=ID,myID={ID}];
myID=({#,Part[myID,#]}&/@Range[Length[myID]]);
mywikicode=(Module[{mydescription,mycitation={}},
(*Reading Online Data*)
mydescription=OEISImport[#[[2]],"Description"];
(*Function Output*)
mycitation="* {{oeis|"<>ToString[#[[2]]]<>"}}: "<>mydescription;
mycitation])&/@myID;
Return[Export[filename,mywikicode,"Text"]];
],
(*Wrong element*)
True,
Message[General::optx,filename,2];
Return[$Failed];
]
]
(* ::Section:: *)
(*(* Exporting b-Files *)*)
OEISbFile[ID_?OEISValidateIDQ,VMax_Integer,filename___]:=Module[{mybfiledata,mymindata,mymaxdata,mybfilename,mybfile},
mymindata=OEISImport[ID,"MinData"];
(*mymaxdata=OEISImport[ID,"MaxData"];*)
(*If no input filename use a default filename*)
If[filename=="Null",mybfilename=OEISURL[ID,URL->False,bFile->True],mybfilename=filename];
(* If the function is not defined load all available data from OEIS*)
If[NameQ[ID]==False,OEISFunction[ID,Output->False]];
mybfiledata=({#,(ToExpression[ID<>"["<>ToString[#]<>"]"])}&/@Range[mymindata,VMax]);
(*Delete non numerical data*)
mybfiledata=Select[mybfiledata,NumberQ[#[[2]]]&];
(* Unicode LF for newlines *)
mybfile=OpenWrite[mybfilename, BinaryFormat->True, CharacterEncoding->"Unicode"];
WriteString[mybfile, ToString[#[[1]]]<>" "<>ToString[#[[2]]]<>"\n"]&/@mybfiledata;
Return[Close[mybfile]]
]
End[];
Protect["`*"];
EndPackage[];