100% found this document useful (3 votes)
18 views

Numeric Computation and Statistical Data Analysis on the Java Platform Advanced Information and Knowledge Processing Chekanov Sergei V pdf download

The document discusses the book 'Numeric Computation and Statistical Data Analysis on the Java Platform' by Sergei V. Chekanov, which focuses on statistical calculations using various programming languages, primarily Java and Python. It aims to provide practical approaches to numerical computations and data analysis, offering over 300 code snippets for data visualization. The book is intended for a broad audience, including students and professionals, and serves as a resource for understanding data analysis and statistical calculations across different programming languages.

Uploaded by

dossasdyettal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
18 views

Numeric Computation and Statistical Data Analysis on the Java Platform Advanced Information and Knowledge Processing Chekanov Sergei V pdf download

The document discusses the book 'Numeric Computation and Statistical Data Analysis on the Java Platform' by Sergei V. Chekanov, which focuses on statistical calculations using various programming languages, primarily Java and Python. It aims to provide practical approaches to numerical computations and data analysis, offering over 300 code snippets for data visualization. The book is intended for a broad audience, including students and professionals, and serves as a resource for understanding data analysis and statistical calculations across different programming languages.

Uploaded by

dossasdyettal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Numeric Computation and Statistical Data

Analysis on the Java Platform Advanced


Information and Knowledge Processing Chekanov
Sergei V download
https://textbookfull.com/product/numeric-computation-and-
statistical-data-analysis-on-the-java-platform-advanced-
information-and-knowledge-processing-chekanov-sergei-v/

Download more ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

Scientific Data Analysis using Jython Scripting and


Java Advanced Information and Knowledge Processing
2010th Edition Chekanov Sergei V

https://textbookfull.com/product/scientific-data-analysis-using-
jython-scripting-and-java-advanced-information-and-knowledge-
processing-2010th-edition-chekanov-sergei-v/

Advanced R Statistical Programming and Data Models:


Analysis, Machine Learning, and Visualization 1st
Edition Matt Wiley

https://textbookfull.com/product/advanced-r-statistical-
programming-and-data-models-analysis-machine-learning-and-
visualization-1st-edition-matt-wiley/

Metaprogramming in R: Advanced Statistical Programming


for Data Science, Analysis and Finance 1st Edition
Thomas Mailund

https://textbookfull.com/product/metaprogramming-in-r-advanced-
statistical-programming-for-data-science-analysis-and-
finance-1st-edition-thomas-mailund/

Real Estate Analysis in the Information Age Techniques


for Big Data and Statistical Modeling 1st Edition
Winson-Geideman

https://textbookfull.com/product/real-estate-analysis-in-the-
information-age-techniques-for-big-data-and-statistical-
modeling-1st-edition-winson-geideman/
Functional Programming in R: Advanced Statistical
Programming for Data Science, Analysis and Finance 1st
Edition Thomas Mailund

https://textbookfull.com/product/functional-programming-in-r-
advanced-statistical-programming-for-data-science-analysis-and-
finance-1st-edition-thomas-mailund/

Advanced Object-Oriented Programming in R: Statistical


Programming for Data Science, Analysis and Finance 1st
Edition Thomas Mailund

https://textbookfull.com/product/advanced-object-oriented-
programming-in-r-statistical-programming-for-data-science-
analysis-and-finance-1st-edition-thomas-mailund/

Statistical Data Analysis using SAS Intermediate


Statistical Methods Mervyn G. Marasinghe

https://textbookfull.com/product/statistical-data-analysis-using-
sas-intermediate-statistical-methods-mervyn-g-marasinghe/

Advanced linear modeling statistical learning and


dependent data 3rd Edition Christensen R

https://textbookfull.com/product/advanced-linear-modeling-
statistical-learning-and-dependent-data-3rd-edition-
christensen-r/

Advanced Information Networking and Applications:


Proceedings of the 33rd International Conference on
Advanced Information Networking and Applications
(AINA-2019) Leonard Barolli
https://textbookfull.com/product/advanced-information-networking-
and-applications-proceedings-of-the-33rd-international-
conference-on-advanced-information-networking-and-applications-
Advanced Information and Knowledge Processing

Sergei V. Chekanov

Numeric
Computation and
Statistical Data
Analysis on the
Java Platform
Advanced Information and Knowledge
Processing

Series editors
Lakhmi C. Jain
Bournemouth University, Poole, UK and
University of South Australia, Adelaide, Australia

Xindong Wu
University of Vermont
Information systems and intelligent knowledge processing are playing an increasing
role in business, science and technology. Recently, advanced information systems
have evolved to facilitate the co-evolution of human and information networks
within communities. These advanced information systems use various paradigms
including artificial intelligence, knowledge management, and neural science as well
as conventional information processing paradigms. The aim of this series is to
publish books on new designs and applications of advanced information and
knowledge processing paradigms in areas including but not limited to aviation,
business, security, education, engineering, health, management, and science. Books
in the series should have a strong focus on information processing—preferably
combined with, or extended by, new results from adjacent sciences. Proposals for
research monographs, reference books, coherently integrated multi-author edited
books, and handbooks will be considered for the series and each proposal will be
reviewed by the Series Editors, with additional reviews from the editorial board and
independent reviewers where appropriate. Titles published within the Advanced
Information and Knowledge Processing series are included in Thomson Reuters’
Book Citation Index.

More information about this series at http://www.springer.com/series/4738


Sergei V. Chekanov

Numeric Computation
and Statistical Data Analysis
on the Java Platform

123
Sergei V. Chekanov
HEP Division
Argonne National Laboratory
Lemont, IL
USA

Additional material to this book can be downloaded from http://extras.springer.com/.

ISSN 1610-3947 ISSN 2197-8441 (electronic)


Advanced Information and Knowledge Processing
ISBN 978-3-319-28529-0 ISBN 978-3-319-28531-3 (eBook)
DOI 10.1007/978-3-319-28531-3

Library of Congress Control Number: 2016932001

© Springer International Publishing Switzerland 2016


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt from
the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, express or implied, with respect to the material contained herein or
for any errors or omissions that may have been made.

Printed on acid-free paper

This Springer imprint is published by SpringerNature


The registered company is Springer International Publishing AG Switzerland
This book is dedicated to my family
Preface

Numerical and statistical algorithms are typically confined within a specific pro-
gramming language. For example, the R open-source data-analysis software uses a
specialized scripting language, which is an implementation of the “S” programming
language. Many commercial mathematical programs follow this trend. This book is
about a platform for statistical calculations using algorithms that are not confined by
a chosen language. For example, this platform allows mixing Python and Java
numerical libraries, or using them on their own. Or, one can use this book to
program statistical code using other languages, such as Groovy, Ruby, and
BeanShell. This book is about an approach to scientific programming and visual-
ization that does not set strict requirements on specific programming languages, nor
on operating systems where such calculations are performed.
There are many books written about Java—one of the most popular program-
ming languages. There are many books written about Python, which is another very
popular programming language. This book explains how to mix them, bringing
incredible algorithmic power and cutting-edge numeric libraries to scientific com-
putations and data visualization.
In this book I did not go deep inside particular scientific research area, since the
aim was to give concrete examples which illustrate which Java libraries should be
used to perform computations. In the cases when I could not cover the subject in
detail, a sufficient number of relevant references was given, so the reader can easily
find necessary information for each chapter using external sources.
Thus this book presents practical approaches to numerical computations, data
analysis, and knowledge discovery, focusing on programming techniques. Each
chapter describes the conceptual underpinning for numerical and statistical calcu-
lations using Java libraries, covering many aspects from simple multidimensional
arrays and histograms to clustering analysis, curve fitting, neural networks, and
symbolic calculations. To make the examples as simple as possible from the
computational point of view, I fully embrace the scripting approach in the course of
this book. This leads to short and clear analysis codes, so you could concentrate on
the logic of analysis flow rather than on language-specific details.

vii
viii Preface

This book uses Python as the main programming language, since it is elegant and
easy to learn. It is a great language for teaching scientific computation. For devel-
opers, this is an ideal language for fast prototyping and debugging. The book dis-
cusses how to design code snippets for numeric computation and statistics on the
Java platform. To be more exact, we will use Jython (Python implemented in Java), a
language that uses not only native Python modules, but can also access very com-
prehensive Java classes. The reader will learn how to write analysis codes, while
numerous code snippets will give you some ideas on numeric algorithms which can
easily be incorporated into realistic research application. The book includes more
than 300 code snippets to produce data-visualization plots in 2D and 3D.
I am almost convinced myself that this book is self-contained and does not
depend on detailed knowledge of computing language, although knowledge of
Python and Java is desirable. However, the reader may still need some programming
background in order to use this book with other languages, such as Groovy,
BeanShell, and Ruby, since I did not give very detailed coverage of these languages.

Who Is This Book for

This book is intended for general audiences, for those who use computing to make
sense of data surrounding us. It can be used as a source of knowledge on data
analysis and statistical calculations for students and professionals of all disciplines.
This book was written for undergraduate and graduate students, academics, pro-
fessors, and professionals of any field and any age. The book could be used as a
textbook for students.
We also hope that this book will be useful for those who study financial markets,
since the numeric algorithms discussed in this book are undoubtedly common to any
knowledge discovery research. This book equips readers with the description of a
computational platform for statistical calculations which can be viewed as an inex-
pensive alternative to costly commercial products used by financial-market analysts.
I assume the readers are not familiar with Python/Jython, the main programming
language used for code snippets in this book. But some basic understanding of
statistics and mathematics would be very helpful to understand the material of this
book.
All example codes of this book can easily be transformed to Java, Groovy,
Ruby/JRuby, or BeanShell codes. You are presumed to have knowledge of pro-
gramming in Java, if you will choose the path of moving the examples to Java, or if
you will decide to create Java libraries to be deployed as jar files for a new project.
The book will discuss how to do this, and a few Java examples will be provided.
Transformations of the example snippets to scripting languages, such as Groovy,
Ruby/JRuby, or BeanShell, may require some knowledge of these scripting
languages. The good thing is that the analysis algorithms and numerical libraries
will be exactly the same, so a little effort is required to move to other languages.
Again, we will show you how to convert Jython codes to these languages. In most
Preface ix

cases, our examples should be sufficient to get started with a new language. The
more knowledge about Groovy and Ruby/JRuby you can bring, the more you will
get out of this book.

Books You May Read Before

The material of this book is self-contained. However, to understand the material


deeper, you may need to look at other sources. First of all, there are plenty of good
books [1–5] on Python and Jython, which are more complete for language-specific
topics than the information given in this book. If you program in Java that forms the
backbone of numerical and graphical libraries discussed in this book, a great deal of
supplementary information can be found in Java books [6–10].
Secondly, there are several books on Groovy, a popular scripting language that
can be used to work with the Java numerical libraries discussed in this book [11,
12]. If your choice is JRuby, the Ruby programming language on the Java platform,
look at these books [13, 14] to get started.
Thirdly, as you read, you may need to look at external sources to understand the
material better, especially when we come to statistical interpretations of data. We will
supply the reader with the necessary references, so he or she can choose the most
appropriate (and affordable) books to discover the world of data analysis and data
mining.

References

1. Pilgrim M (2004), Dive into Python, Apress


2. Guzdial M (2005) Introduction to computing and programming in Python, a multimedia
approach, Prentice Hall
3. Martelli A (2006) Python in a nutshell (in a nutshell (O’Reilly)), O’Reilly Media, Inc.
4. Lutz M (2007) Learning Python, 3rd edn. O’Reilly Media, Inc.
5. Langtangen H (2008) Python scripting for computational science. Springer-Verlag, Berlin,
Heidelberg
6. Richardson C, Avondolio D, Vitale J et al (2005) Professional Java, JDK 5 Edition, Wrox
7. Arnold K, Gosling J, Holmes D (2005) Java(TM) programming language. In: The Java Series,
4th edn. Addison-Wesley Professional
8. Flanagan D (2005) Java in a nutshell, 5th edn. O’Reilly Media, Inc.
9. Eckel B (2006) Thinking in Java, 4th edn. Prentice Hall PTR
10. Bloch J (2008) Effective Java. In: The Java Series, 2nd edn. Prentice Hall PTR
11. Subramaniam V (2013) Programming Groovy 2: dynamic productivity for the Java
Developer, Pragmatic Programmers, LLC
12. King D, Glover A (2013) Groovy in action, Manning Publications, 2007, https://www.
manning.com/books/groovy-in-action
13. Edelson J, Liu H (2008) JRuby cookbook, O’Reilly Media
14. Bini O (2007) Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java,
Apress
Acknowledgements

This book describes a software which is a collective work of many developers who
have dedicated themselves to scientific computing. The author is grateful to all
people who contributed to scientific software, and for their inspiration and dedi-
cation to science and knowledge-discovery software.
Many numeric and graphic libraries discussed in this book were released as
open-source projects. I am grateful to the authors of such open-source programs for
their enthusiasm to share their work, and for making their software publicly
available.
You can find a list of contributions to the software packages described in this
book on the jWork.ORG web page (http://jwork.org/dmelt/). A special note of
thanks to those of you who reported bugs in a constructive way, helped with
solutions, and shared your knowledge and experience with others.
Much of this project grew out of fruitful collaboration with many of my col-
leagues who devoted themselves to high energy physics. Over the course of the past
twenty-five years I have learned a lot about programming aspects of scientific
research. I would like to thank my colleagues for checking and debugging the
examples shown in this book, and here the list will be endless.
I would like to thank everyone at Springer for their help with the production
process. In particular, managing editors H. Desmond and J. Robinson, who helped
start this book in its present form.
Not least, personal thanks go to my dear wife, Tania, and my sons, Alexey
(Alosha) and Roman, for their love and patience to a husband and father who was
only half (mentally) present after coming from his work. Without their patience and
understanding, this book would not have been possible. Finally, I also thank my
parents and sister for their support of my interests in all aspects of science.

Chicago Sergei V. Chekanov


January 2016

xi
Contents

1 Java Computational Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Programming in Java. . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 The DMelt Software Platform. . . . . . . . . . . . . . . . 3
1.1.3 Some Warnings . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.4 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Scripting with DMelt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.1 Learning by Example . . . . . . . . . . . . . . . . . . . . . 7
1.2.2 Using Jython for Code Examples . . . . . . . . . . . . . 8
1.2.3 Differences with Other Math Software . . . . . . . . . . 9
1.3 Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 DMelt Workbench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.1 Source Code Editor . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.2 DMelt Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.3 Jython and BeanShell Consoles . . . . . . . . . . . . . . 15
1.4.4 Accessing Methods of Instances . . . . . . . . . . . . . . 17
1.4.5 Editing Jython Scripts . . . . . . . . . . . . . . . . . . . . . 17
1.4.6 Running Jython Scripts . . . . . . . . . . . . . . . . . . . . 17
1.4.7 Macro Files of the DMelt IDE . . . . . . . . . . . . . . . 18
1.4.8 Running BeanShell Scripts . . . . . . . . . . . . . . . . . . 19
1.4.9 Compiling and Running Java Code . . . . . . . . . . . . 19
1.4.10 DMelt Code Assist . . . . . . . . . . . . . . . . . . . . . . . 20
1.4.11 Other Features . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.4.12 Working with Images . . . . . . . . . . . . . . . . . . . . . 23
1.4.13 DMelt License . . . . . . . . . . . . . . . . . . . . . . . . . . 24
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

xiii
xiv Contents

2 Introduction to Jython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1 Code Structure and Jython Objects . . . . . . . . . . . . . . . . . . . 27
2.1.1 Numbers as Objects . . . . . . . . . . . . . . . . . . . . . . 30
2.1.2 Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . 32
2.1.3 Mathematical Functions . . . . . . . . . . . . . . . . . . . . 32
2.2 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3 Strings as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.4 Import Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4.1 Executing Native Applications . . . . . . . . . . . . . . . 37
2.5 Comparison Tests and Loops . . . . . . . . . . . . . . . . . . . . . . . 38
2.5.1 The “if-else” Statement . . . . . . . . . . . . . . . . . . . . 38
2.5.2 Loops. The “for” Statement . . . . . . . . . . . . . . . . . 39
2.5.3 The “continue” and “break” Statements . . . . . . . . . 39
2.5.4 Loops. The “while” Statement . . . . . . . . . . . . . . . 40
2.6 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.6.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.6.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.6.3 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.6.4 Functional Programming . . . . . . . . . . . . . . . . . . . 48
2.7 Java Collections in Jython . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.7.1 List. An Ordered Collection . . . . . . . . . . . . . . . . . 50
2.7.2 Set. A Collection Without Duplicate Elements . . . . 53
2.7.3 SortedSet. Sorted Unique Elements . . . . . . . . . . . . 54
2.7.4 Map. Mapping Keys to Values . . . . . . . . . . . . . . . 55
2.7.5 Java Map with Sorted Elements . . . . . . . . . . . . . . 55
2.7.6 Real-Life Example: Sorting and Removing
Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.8 Random Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.9 Time Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.9.1 Benchmarking . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.10 Python Functions and Modules. . . . . . . . . . . . . . . . . . . . . . 60
2.11 Python Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.11.1 Initializing a Class . . . . . . . . . . . . . . . . . . . . . . . 65
2.11.2 Classes Inherited from Other Classes. . . . . . . . . . . 65
2.11.3 Java Classes in Jython . . . . . . . . . . . . . . . . . . . . . 66
2.11.4 Not Covered Topics . . . . . . . . . . . . . . . . . . . . . . 66
2.12 Parallel Computing and Threads . . . . . . . . . . . . . . . . . . . . . 67
2.13 Arrays in Jython. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.13.1 Array Conversion and Transformations . . . . . . . . . 69
2.13.2 Performance Issues . . . . . . . . . . . . . . . . . . . . . . . 69
2.13.3 Used Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 70
2.14 Exceptions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Contents xv

2.15 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72


2.15.1 User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.15.2 Reading and Writing Files . . . . . . . . . . . . . . . . . . 72
2.15.3 Input and Output for Arrays . . . . . . . . . . . . . . . . . 74
2.15.4 Working with CSV Python Module. . . . . . . . . . . . 74
2.15.5 Saving Objects in a Serialized File . . . . . . . . . . . . 76
2.15.6 Storing Multiple Objects . . . . . . . . . . . . . . . . . . . 77
2.15.7 Using Java for I/O . . . . . . . . . . . . . . . . . . . . . . . 78
2.15.8 Reading Data from the Network . . . . . . . . . . . . . . 79
2.16 Real-Life Example. Collecting Data Files . . . . . . . . . . . . . . 80
2.17 Using Java for GUI Programming. . . . . . . . . . . . . . . . . . . . 82
2.18 Concluding Remarks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.1 Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.2 Functions in DMelt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2.1 Java Implementation of F1D . . . . . . . . . . . . . . . . 89
3.2.2 Manipulations with 1D Functions . . . . . . . . . . . . . 90
3.3 Plotting 1D Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.3.1 Building a Graphical Canvas . . . . . . . . . . . . . . . . 93
3.3.2 Drawing 1D Functions . . . . . . . . . . . . . . . . . . . . 97
3.3.3 Plotting Functions on Different Pads . . . . . . . . . . . 99
3.3.4 Short Summary of HPlot Methods . . . . . . . . . . . . 99
3.3.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.4 2D Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.4.1 Functions in Two Dimensions . . . . . . . . . . . . . . . 102
3.4.2 Displaying 2D Functions . . . . . . . . . . . . . . . . . . . 103
3.4.3 Using a Contour Plot. . . . . . . . . . . . . . . . . . . . . . 105
3.5 3D Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.5.1 Functions in Three Dimensions. . . . . . . . . . . . . . . 106
3.6 Functions in Many Dimensions . . . . . . . . . . . . . . . . . . . . . 107
3.6.1 FND Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 107
3.6.2 Drawing FND Functions . . . . . . . . . . . . . . . . . . . 108
3.7 Custom Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
3.7.1 Custom Functions and Their Methods . . . . . . . . . . 109
3.7.2 Custom Functions Using Expression Builder . . . . . 112
3.7.3 Custom Functions in Jython . . . . . . . . . . . . . . . . . 114
3.8 Parametric Surfaces in 3D . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.8.1 FPR Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.8.2 3D Mathematical Objects . . . . . . . . . . . . . . . . . . . 122
xvi Contents

3.9 Function Minimization . . . . . . . . . . . . . . . . . . . . . . . . . . . 122


3.9.1 Minimization of Multidimensional Functions . . . . . 125
3.9.2 Calling Migrad Directly . . . . . . . . . . . . . . . . . . . . 126
3.10 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
4 Data Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.1 1D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.1.1 P0D Data Container . . . . . . . . . . . . . . . . . . . . . . 132
4.1.2 P0D Transformations. . . . . . . . . . . . . . . . . . . . . . 135
4.1.3 Statistical Summary. . . . . . . . . . . . . . . . . . . . . . . 136
4.1.4 Displaying P0D Data. . . . . . . . . . . . . . . . . . . . . . 138
4.1.5 File Input and Output . . . . . . . . . . . . . . . . . . . . . 139
4.2 Arrays for Two Dimensions . . . . . . . . . . . . . . . . . . . . . . . . 143
4.2.1 Data with Errors . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.2.2 Viewing P1D Data . . . . . . . . . . . . . . . . . . . . . . . 150
4.2.3 Plotting P1D Data. . . . . . . . . . . . . . . . . . . . . . . . 151
4.2.4 Contour Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.2.5 Manipulations with P1D Data. . . . . . . . . . . . . . . . 153
4.2.6 Advanced P1D Operations . . . . . . . . . . . . . . . . . . 155
4.2.7 Weighted Average and Systematical
Uncertainties . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.2.8 File Input and Output . . . . . . . . . . . . . . . . . . . . . 159
4.2.9 Example I: Henon Attractor . . . . . . . . . . . . . . . . . 162
4.2.10 Example II. Weighted Average . . . . . . . . . . . . . . . 163
4.3 Other Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.3.1 P2D Data Container . . . . . . . . . . . . . . . . . . . . . . 166
4.3.2 P3D Data Container . . . . . . . . . . . . . . . . . . . . . . 169
4.3.3 PND Data Container . . . . . . . . . . . . . . . . . . . . . . 171
4.3.4 File Input and Output . . . . . . . . . . . . . . . . . . . . . 174
4.4 Third-Party Data Containers. . . . . . . . . . . . . . . . . . . . . . . . 174
4.4.1 Math Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
4.4.2 Jaida Data Containers . . . . . . . . . . . . . . . . . . . . . 175
4.4.3 jMathTools Arrays . . . . . . . . . . . . . . . . . . . . . . . 178
4.4.4 Colt Data Containers . . . . . . . . . . . . . . . . . . . . . . 181
4.4.5 Lorentz Vector . . . . . . . . . . . . . . . . . . . . . . . . . . 182
4.5 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 185
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
5 Linear Algebra and Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 187
5.1 Vector and Matrix Packages . . . . . . . . . . . . . . . . . . . . . . . 187
5.1.1 Basic Matrix Arithmetic. . . . . . . . . . . . . . . . . . . . 189
5.1.2 Elements of Linear Algebra . . . . . . . . . . . . . . . . . 190
5.1.3 Jampack Matrix Computations . . . . . . . . . . . . . . . 191
5.1.4 La4J Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Contents xvii

5.1.5 EJML Matrix Library . . . . . . . . . . . . . . . . . . . . . 195


5.1.6 Multithreaded Matrix Computations . . . . . . . . . . . 196
5.1.7 JBlas and Other Matrix Packages . . . . . . . . . . . . . 199
5.1.8 Python Vector and Matrix Operations . . . . . . . . . . 199
5.1.9 Matrix Operations in SymPy . . . . . . . . . . . . . . . . 201
5.2 Algebraic Manipulations with Tensors. . . . . . . . . . . . . . . . . 202
5.3 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
5.3.1 Polynomial Equations . . . . . . . . . . . . . . . . . . . . . 203
5.3.2 Linear Systems of Equations . . . . . . . . . . . . . . . . 204
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6 Symbolic Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.1 Using the Octave Language . . . . . . . . . . . . . . . . . . . . . . . . 207
6.2 Java Symbolic Computing Library . . . . . . . . . . . . . . . . . . . 208
6.2.1 Conversion to Elementary Functions . . . . . . . . . . . 210
6.2.2 Numeric Calculations . . . . . . . . . . . . . . . . . . . . . 210
6.2.3 Simplify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
6.2.4 Substitutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.2.5 Differentiate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.2.6 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.2.7 Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
6.2.8 MathML Output . . . . . . . . . . . . . . . . . . . . . . . . . 213
6.2.9 Integration with DMelt Plotting Canvases . . . . . . . 213
6.3 Using SymPy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
7 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
7.1 One-Dimensional Histogram . . . . . . . . . . . . . . . . . . . . . . . 220
7.1.1 Probability Distribution and Probability Density . . . 224
7.1.2 Histogram Characteristics. . . . . . . . . . . . . . . . . . . 225
7.1.3 Initialization and Filling Methods . . . . . . . . . . . . . 225
7.1.4 Accessing Histogram Values . . . . . . . . . . . . . . . . 228
7.1.5 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
7.1.6 Histogram Operations . . . . . . . . . . . . . . . . . . . . . 230
7.1.7 Accessing Low-Level Jaida Classes. . . . . . . . . . . . 231
7.1.8 Graphical Attributes . . . . . . . . . . . . . . . . . . . . . . 232
7.2 Histogram in 2D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
7.2.1 Histogram Operations . . . . . . . . . . . . . . . . . . . . . 236
7.2.2 Graphical Representation . . . . . . . . . . . . . . . . . . . 236
7.3 Histograms in Jaida . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
7.4 Histogram in 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
7.5 Profile Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
xviii Contents

7.6 Histogram Input and Output. . . . . . . . . . . . . . . . . . . . . . . . 244


7.6.1 External Programs for Histograms. . . . . . . . . . . . . 246
7.7 Analyzing Histograms from Multiple Files. . . . . . . . . . . . . . 247
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
8 Scientific visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8.1 Graphical Canvases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8.2 HPlot Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
8.2.1 Working with the HPlot Canvas . . . . . . . . . . . . . . 255
8.2.2 Saving Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
8.2.3 Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
8.2.4 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
8.2.5 Summary of the HPlot Methods . . . . . . . . . . . . . . 259
8.2.6 Exporting to Image Files . . . . . . . . . . . . . . . . . . . 261
8.2.7 Labels and Keys . . . . . . . . . . . . . . . . . . . . . . . . . 262
8.2.8 Geometrical Primitives. . . . . . . . . . . . . . . . . . . . . 265
8.2.9 Text Strings and Symbols . . . . . . . . . . . . . . . . . . 266
8.3 Interconnected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
8.4 Showing Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
8.5 Lightweight Canvases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
8.5.1 Henon Attractor Again . . . . . . . . . . . . . . . . . . . . 275
8.6 Canvas for Interactive Drawing . . . . . . . . . . . . . . . . . . . . . 276
8.6.1 Drawing Diagrams . . . . . . . . . . . . . . . . . . . . . . . 277
8.6.2 SHPlotJa Class . . . . . . . . . . . . . . . . . . . . . . . . . . 279
8.7 Custom Plotting in XY . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
8.7.1 HPlotXY Canvas . . . . . . . . . . . . . . . . . . . . . . . . 279
8.7.2 WPlot Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . 280
8.7.3 HPlotJas Canvas . . . . . . . . . . . . . . . . . . . . . . . . . 281
8.8 HPlot2D Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
8.9 Visualization in 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
8.9.1 HPlot3D Canvas . . . . . . . . . . . . . . . . . . . . . . . . . 285
8.9.2 HPlot3DP Canvas . . . . . . . . . . . . . . . . . . . . . . . . 285
8.9.3 Mathematical Objects in 3D . . . . . . . . . . . . . . . . . 289
8.10 Plotting Real-Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . 291
8.10.1 Real-Time Data Using SPlot . . . . . . . . . . . . . . . . 292
8.10.2 Real-Time Data Using HPlotRT . . . . . . . . . . . . . . 293
8.11 Graphs and Java GUI Components . . . . . . . . . . . . . . . . . . . 293
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
9 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
9.1 Nonpersistent Data: Memory-Based Data. . . . . . . . . . . . . . . 297
9.2 Object Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
9.3 Persistent Event Records . . . . . . . . . . . . . . . . . . . . . . . . . . 300
9.3.1 Sequential Input and Output . . . . . . . . . . . . . . . . . 300
9.3.2 Opening Data in a Browser . . . . . . . . . . . . . . . . . 302
Contents xix

9.3.3 Saving Event Records Persistently. . . . . . . . . . . . . 303


9.3.4 Buffer Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
9.3.5 XML File Format . . . . . . . . . . . . . . . . . . . . . . . . 305
9.4 PFile Data Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
9.4.1 Browser for PFile File Data . . . . . . . . . . . . . . . . . 308
9.5 HBook XML Data Output . . . . . . . . . . . . . . . . . . . . . . . . . 309
9.6 Text File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
9.6.1 Working with ASCII Files . . . . . . . . . . . . . . . . . . 311
9.6.2 CSV File Format . . . . . . . . . . . . . . . . . . . . . . . . 312
9.6.3 EDN File Format . . . . . . . . . . . . . . . . . . . . . . . . 315
9.6.4 DIF File Format . . . . . . . . . . . . . . . . . . . . . . . . . 316
9.7 Reading ROOT and AIDA Files. . . . . . . . . . . . . . . . . . . . . 317
9.7.1 ROOT Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
9.7.2 AIDA Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
9.8 Google’s Protocol Buffer Format . . . . . . . . . . . . . . . . . . . . 318
9.8.1 Prototyping Data Records . . . . . . . . . . . . . . . . . . 319
9.8.2 Dealing with Data Using Java . . . . . . . . . . . . . . . 321
9.8.3 Switching to Jython . . . . . . . . . . . . . . . . . . . . . . 323
9.8.4 Adding New Data Records. . . . . . . . . . . . . . . . . . 324
9.8.5 Using C++ with the Protocol Buffers. . . . . . . . . . . 325
9.8.6 Some Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . 327
9.9 Creating Excel Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
9.10 Non-SQL Object Databases . . . . . . . . . . . . . . . . . . . . . . . . 330
9.10.1 Nonsequential Input and Output . . . . . . . . . . . . . . 330
9.10.2 Persistent Map . . . . . . . . . . . . . . . . . . . . . . . . . . 332
9.10.3 MapDB Database . . . . . . . . . . . . . . . . . . . . . . . . 333
9.10.4 NeoDatis Database . . . . . . . . . . . . . . . . . . . . . . . 334
9.11 Relational SQL Databases . . . . . . . . . . . . . . . . . . . . . . . . . 335
9.11.1 Derby SQL Database. . . . . . . . . . . . . . . . . . . . . . 336
9.11.2 HyperSQL Database . . . . . . . . . . . . . . . . . . . . . . 341
9.11.3 SQLite Database . . . . . . . . . . . . . . . . . . . . . . . . . 342
9.12 Miscellaneous Input–Output Topics . . . . . . . . . . . . . . . . . . 343
9.12.1 Building List of Files . . . . . . . . . . . . . . . . . . . . . 343
9.12.2 Reading Configuration Files . . . . . . . . . . . . . . . . . 344
9.13 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
9.13.1 Dealing with Single Objects . . . . . . . . . . . . . . . . . 347
9.13.2 Dealing with Object Collections . . . . . . . . . . . . . . 347
9.13.3 Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
9.13.4 Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
10 Probability and Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
10.1 Descriptive Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
10.1.1 Comparing Data . . . . . . . . . . . . . . . . . . . . . . . . . 355
10.2 Statistical Analysis Using Python . . . . . . . . . . . . . . . . . . . . 356
xx Contents

10.3 Random Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359


10.3.1 Using Random Numbers . . . . . . . . . . . . . . . . . . . 360
10.3.2 Random Numbers in Colt . . . . . . . . . . . . . . . . . . 362
10.3.3 Other Packages with Random Numbers . . . . . . . . . 364
10.4 Random Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
10.4.1 Methods of 1D Arrays. . . . . . . . . . . . . . . . . . . . . 368
10.4.2 Methods of 2D Arrays. . . . . . . . . . . . . . . . . . . . . 370
10.4.3 Sampling Using the Colt Package . . . . . . . . . . . . . 372
10.5 Statistical Significance and Confidence Levels . . . . . . . . . . . 373
10.5.1 Statistical Significance . . . . . . . . . . . . . . . . . . . . . 373
10.5.2 Discovery Sensitivity. . . . . . . . . . . . . . . . . . . . . . 376
10.5.3 Confidence Interval . . . . . . . . . . . . . . . . . . . . . . . 377
10.5.4 Confidence Levels for Small Statistics . . . . . . . . . . 378
10.5.5 Statistical Tests. . . . . . . . . . . . . . . . . . . . . . . . . . 382
10.5.6 Confidence Levels for Distributions. . . . . . . . . . . . 385
10.6 Error Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
10.6.1 Propagation Using Monte Carlo Technique . . . . . . 395
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
11 Linear Regression and Curve Fitting . . . . . . . . . . . . . . . . . . . . . 399
11.1 Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
11.1.1 Creating Input Data. . . . . . . . . . . . . . . . . . . . . . . 400
11.1.2 Performing a Linear Regression . . . . . . . . . . . . . . 400
11.2 Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
11.2.1 Preparing a Fit . . . . . . . . . . . . . . . . . . . . . . . . . . 404
11.2.2 Creating a Fit Function . . . . . . . . . . . . . . . . . . . . 406
11.2.3 Displaying Fit Functions . . . . . . . . . . . . . . . . . . . 410
11.2.4 Making a Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
11.3 Real-Life Example. Signal Plus Background . . . . . . . . . . . . 413
11.3.1 Preparing a Data Sample . . . . . . . . . . . . . . . . . . . 413
11.3.2 Performing Curve Fitting . . . . . . . . . . . . . . . . . . . 413
11.3.3 Fitting Multiple Peaks . . . . . . . . . . . . . . . . . . . . . 415
11.3.4 Fitting Histograms in 3D . . . . . . . . . . . . . . . . . . . 417
11.4 Interactive Fit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
11.4.1 HFit Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
11.4.2 JAS Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
11.5 Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
11.6 Advanced Data Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
11.7 Fitting Using Parametric Equations . . . . . . . . . . . . . . . . . . . 425
11.8 Symbolic Regression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
12 Data Analysis and Data Mining . . . . . . . . . . . . . . . . . . . . . . . . . 431
12.1 First Steps in Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . 432
Contents xxi

12.2 Real Life Example. Analyzing a Gene Catalog . . . ..... . . . 434


12.2.1 Data Transformation . . . . . . . . . . . . . . ..... . . . 435
12.2.2 Data Skimming. . . . . . . . . . . . . . . . . . ..... . . . 435
12.2.3 Data Slimming . . . . . . . . . . . . . . . . . . ..... . . . 436
12.2.4 Data Sorting. . . . . . . . . . . . . . . . . . . . ..... . . . 436
12.2.5 Removing Duplicate Records . . . . . . . . ..... . . . 438
12.2.6 Sorting and Duplicate Removal in Java . ..... . . . 439
12.3 Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..... . . . 440
12.3.1 Using Built-In Metadata File . . . . . . . . ..... . . . 441
12.3.2 External Metadata Files . . . . . . . . . . . . ..... . . . 444
12.4 Multithreaded Programming . . . . . . . . . . . . . . . . ..... . . . 445
12.4.1 Reading Data in Parallel . . . . . . . . . . . ..... . . . 446
12.4.2 Reading a Single File in Parallel . . . . . . ..... . . . 448
12.4.3 Numerical Computations Using Multiple Cores. . . . 451
12.5 Data Consistency and Security . . . . . . . . . . . . . . ..... . . . 452
12.5.1 MD5 Fingerprint at Runtime . . . . . . . . ..... . . . 453
12.5.2 Fingerprinting Files . . . . . . . . . . . . . . . ..... . . . 454
12.6 Real-Life Examples . . . . . . . . . . . . . . . . . . . . . ..... . . . 455
12.6.1 Single-Particle Densities . . . . . . . . . . . ..... . . . 455
12.6.2 Fluctuations and Correlations . . . . . . . . ..... . . . 458
12.6.3 Analyzing Nearby Galaxies . . . . . . . . . ..... . . . 468
12.6.4 Analyzing Elementary Particles. . . . . . . ..... . . . 470
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..... . . . 473
13 Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
13.2 A Basic Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . 476
13.2.1 Encog Approach . . . . . . . . . . . . . . . . . . . . . . . . . 477
13.2.2 Using Neuroth . . . . . . . . . . . . . . . . . . . . . . . . . . 479
13.3 Backpropagation with Multiple Outputs. . . . . . . . . . . . . . . . 480
13.4 Numeric Predictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
13.4.1 Generating a Data Sample . . . . . . . . . . . . . . . . . . 483
13.4.2 Data Preparation . . . . . . . . . . . . . . . . . . . . . . . . . 484
13.4.3 Building a Neural Net . . . . . . . . . . . . . . . . . . . . . 486
13.4.4 Training and Verifying . . . . . . . . . . . . . . . . . . . . 488
13.5 Bayesian Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
13.5.1 Creating Bayesian Network Using Scripts . . . . . . . 491
13.5.2 HBayes Method . . . . . . . . . . . . . . . . . . . . . . . . . 494
13.6 Kohonen Self-organizing Map . . . . . . . . . . . . . . . . . . . . . . 494
13.6.1 Kohonen SOFM in 2D . . . . . . . . . . . . . . . . . . . . 495
13.6.2 Kohonen SOFM in 3D . . . . . . . . . . . . . . . . . . . . 497
xxii Contents

13.7 Bayesian Self-organizing Map . . . . . . . . . . . . . . . . . . . . . . 498


13.7.1 Noninteractive BSOM . . . . . . . . . . . . . . . . . . . . . 500
13.8 Neural Network Using Python Libraries . . . . . . . . . . . . . . . 502
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
14 Finding Regularities and Data Classification . . . . . . . . . . . . . . . . 505
14.1 Cluster Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
14.1.1 Preparing a Data Sample . . . . . . . . . . . . . . . . . . . 507
14.1.2 Clustering Analysis . . . . . . . . . . . . . . . . . . . . . . . 509
14.1.3 Interactive Clustering Analysis . . . . . . . . . . . . . . . 512
14.2 Clustering Particles into Jets. Real-Life Example . . . . . . . . . 513
14.3 Smoothing and Interpolation . . . . . . . . . . . . . . . . . . . . . . . 515
14.4 Peak Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
14.5 Principal Component Analysis . . . . . . . . . . . . . . . . . . . . . . 522
14.6 Decision Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
15 Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
15.1 Working with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
15.1.1 Downloading Files from the Web . . . . . . . . . . . . . 527
15.1.2 Extracting Data from Figures . . . . . . . . . . . . . . . . 528
15.1.3 Tables and Spreadsheets . . . . . . . . . . . . . . . . . . . 529
15.2 Measurements with Units . . . . . . . . . . . . . . . . . . . . . . . . . 531
15.3 Cellular Automaton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
15.4 Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
15.4.1 Image Modification . . . . . . . . . . . . . . . . . . . . . . . 537
15.4.2 Transforms Using Multiple Cores . . . . . . . . . . . . . 538
15.5 Market and Financial Analysis . . . . . . . . . . . . . . . . . . . . . . 539
15.5.1 Time Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
15.5.2 Financial Charts . . . . . . . . . . . . . . . . . . . . . . . . . 544
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
16 Using Other Languages on the Java Platform . . . . . . . . . . . . . . . 547
16.1 Python Scripting with DMelt . . . . . . . . . . . . . . . . . . . . . . . 547
16.1.1 Operations with Data Holders. . . . . . . . . . . . . . . . 550
16.1.2 Adding Python Modules . . . . . . . . . . . . . . . . . . . 551
16.2 Using Java Programming. . . . . . . . . . . . . . . . . . . . . . . . . . 551
16.2.1 External Java Libraries . . . . . . . . . . . . . . . . . . . . 554
16.2.2 Working Java Projects . . . . . . . . . . . . . . . . . . . . . 554
16.2.3 Embedding DMelt in Applets . . . . . . . . . . . . . . . . 555
16.3 Using BeanShell Language . . . . . . . . . . . . . . . . . . . . . . . . 558
16.4 Using Groovy Language . . . . . . . . . . . . . . . . . . . . . . . . . . 560
16.5 Using Ruby Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
16.6 Using Octave Language . . . . . . . . . . . . . . . . . . . . . . . . . . 564
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Contents xxiii

17 Octave-Style Scripting Using Java. . . . . . . . . . . . . . . . . . . . . . . . 567


17.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
17.2 Variables and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 569
17.2.1 Symbolic Variables . . . . . . . . . . . . . . . . . . . . . . . 572
17.2.2 Operators and Commands . . . . . . . . . . . . . . . . . . 573
17.3 Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
17.4 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
17.5 Vectors and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
17.5.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
17.5.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
17.6 Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
17.6.1 Benchmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
17.6.2 File Input and Output . . . . . . . . . . . . . . . . . . . . . 590
17.7 Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
17.7.1 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . 590
17.7.2 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
17.7.3 Indefinite Integral . . . . . . . . . . . . . . . . . . . . . . . . 592
17.7.4 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . 593
17.7.5 Simplifying Expressions . . . . . . . . . . . . . . . . . . . 594
17.8 Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
17.8.1 Plotting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
17.8.2 Plot2D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
17.8.3 Plot3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
17.9 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
17.9.1 Systems of Linear Equations . . . . . . . . . . . . . . . . 604
17.9.2 Nonlinear Equations . . . . . . . . . . . . . . . . . . . . . . 605
17.9.3 Systems of Equations . . . . . . . . . . . . . . . . . . . . . 606
17.9.4 Differential Equations . . . . . . . . . . . . . . . . . . . . . 607
17.10 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
17.10.1 Descriptive Statistics . . . . . . . . . . . . . . . . . . . . . . 608
17.10.2 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . 610
17.10.3 Data Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
17.10.4 Histograms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
17.11 Again About Integration with Java . . . . . . . . . . . . . . . . . . . 611
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Conventions and Acronyms

This book uses the following typographical convention: A box with a code inside
usually means interactive Python/Jython commands typed in the “Jython Shell.” All
such commands start with the symbol [[[ which is the usual invitation in
Python to type a command. This is shown in the example below:

Working interactively with the Jython prompt has the drawback that it is
impossible to save typed commands. In most cases, the code snippets are not so
short, although they are still much shorter than in any other programming language.
Therefore, it is desirable to save the typed code in a file for further modification and
execution. In this case, we use Jython macro files, i.e., we write a code using the
DMelt (or any other) editor [15], save it in a file with the extension “.py”, and run it
using the keyboard shortcut [F8] or the button “run” from the DMelt tool bar
menu. Such code examples are also shown inside the box, but code lines do not
start with the Python invitation symbol [[[ . In such situations, the example
codes will be shown as:

For examples written in the Python language, double quotes and apostrophe are
interchangeable. For Java and other languages, this is not the case. So, to make our
code to be easily convertible to Java or Groovy, we will use double quotes around
strings. As in the above example, we will try to comment code lines as much as we
can. For Python, comments are preceded by the hash character.
If a code snippet is used as a Python/Jython module by other programs, then we
should write our code inside a file. A Python code always imports an external
module using its file name. Since the file names are important, we will indicate
exactly which file name should be used under the box with a code. For example, if a
program code is considered a module that has to be imported by another code
example, we will show it as:

xxv
xxvi Conventions and Acronyms

Listing 1 File “hello.py”

with the description indicating the file name. For instance,

imports the file “hello.py” and executes it, printing the string. In other cases, we
will use arbitrary file names for the code snippets.
We use typewriter font for Jython and Java classes and methods. For file
names and directories, we also use the same font style with additional parentheses.
We remind that the directory name separators are backward slashes for
Windows, and slashes for Linux and Mac computers. For example, the directory
with examples will be shown as:
macro/examples/
For Windows computers, the same directory should be shown as:
macro\examples\
The dots in this example are used to indicate the upper-level directory.
We will try to avoid using abbreviations. When we use abbreviations, we will
explain their meaning directly in the text. When space allows, we will use mean-
ingful names for variables. This is all.
Chapter 1
Java Computational Platform

1.1 Introduction

Java is both a programming language and a computing platform which runs Java
code. This book uses both. But the Java programming language is not necessary
for the approach adopted in this book, since the Java platform allows the usage of
scripting languages, such as Jython/Python, Groovy, Ruby/JRuby, BeanShell, and
others.
The heart of the Java platform is the Java Virtual Machine (JVM) that runs
programs converted to Java bytecode programs. The conversion to bytecode is done
by Java compiler. Bytecode is the optimized and effective machine language of
JVM. The JVM reads this bytecode, interprets it, and executes the program.
In fact, even if you write your code using other programming languages, such as
Python and Groovy, which are simpler than the Java language, your code still will
be converted to Java bytecode programs.
The JVM is ported to different platforms and insulates the program from the
underlying hardware and operating system. Thus it provides hardware- and
operating-system independence. The Java application programming interface (API)
is also a part of the Java platform. Java API classes are used for building software
applications.

1.1.1 Programming in Java

First, let us discuss the Java programming language, one of the most popular
object-oriented programming languages in use. The statistics of SourceForge reports
that the number of open-source applications written in Java is close to those
written in C++. According to the TIOBE software index (http://www.tiobe.com/), a


c Springer International Publishing Switzerland 2016 1
S.V. Chekanov, Numeric Computation and Statistical Data Analysis
on the Java Platform, Advanced Information and Knowledge Processing,
DOI 10.1007/978-3-319-28531-3_1
2 1 Java Computational Platform

programming-language popularity website, Java is among the most popular object-


oriented languages at the time when this book is written.
Let us briefly discuss the main features of Java that make this language number
one in the industry:
• Java is multi-platform with the philosophy of “write once, run anywhere”;
• Well structured, clean, efficient, simpler (no pointers);
• Stable, robust, and well supported: Java programs written (or compiled) many
years from now can be compiled (or executed) without modifications even today.
This is true even for Java source code with graphic widgets. In contrast, C/C++
programs usually require continuous time-consuming maintenance in order to
follow up the development of C++ compilers and graphic desktop environment;
• Java has the reflection technology, which is not present in C++. The reflec-
tion allows an application to discover information about created objects, thus a
program can design itself at runtime. In particular, this is considered to be an
essential feature for building integrated development environments (IDEs);
• Java has several “intelligent” IDEs, which are indispensable tools for large soft-
ware projects. Some of them, such as Eclipse or Netbeans, are free. We probably
should note that one can also use free IDEs for C/C++, but they are not as intelli-
gent as those for Java and usually miss many important features;
• Automatic garbage collection: Having in hand this feature, a programmer does
not need to perform a low-level memory management;
• Extensive compile-time and runtime checking;
• Java always passes references to objects instead of objects themselves, therefore,
independent of how you program in Java, your code will be rather efficient;
• Java is truly multithreaded. This significantly simplifies the development of appli-
cations that should run in parallel on multi-core machines;
• Advanced serialization: Almost any Java object can be written/retrieved to/from
a file;
• Programs written in Java can be embedded to the Web. This is important for dis-
tributed analysis environment (Java WebStart, plugins, applets), especially when
data-analysis tools are not localized in one single place but scattered over the Web
(nowadays, this is the most common situation).
Sometimes, one can hear that Java is slower than C++. The subject itself is con-
troversial, since the answer totally depends on the nature and the goal of an applica-
tion. Nowadays, most people agree that after introduction of Just-in-Time compiler
(JIT), Java is as fast as C++. Probably, in some areas, Java is still slower than C++,
but the nature of such controversy is already a sign that the performance gap is now
quite small and there is no alarming difference in speed between Java and C++ pro-
grams. And anyway, the proper comparisons with C++ is usually unfair: Java does
a tremendous amount of runtime checks, such as array bound checking, thread syn-
chronization, runtime checking, garbage collection, etc., to make sure that a Java
code runs without problems, and without putting extra stress on a programmer.
Execution of a code on the JVM has a feel of “slowness” since it takes a few mil-
liseconds for the JVM to startup. But when JVM runs a bytecode code, the execution
1.1 Introduction 3

is as fast as for any other program. The JIT compilation converts Java bytecode into
native machine code at runtime. The conversion step can be slow; however, this
does not matter as much for numerical calculations involving large loops due to JIT
compilation.
One should however mention that Java uses more memory than C or FORTRAN.
The main reason—JVM does a lot of internal bookkeeping for garbage collection,
program optimization at runtime, and providing a safeguard for the Java program.
Well, it is better to assign such tasks to the JVM—people who need to use Java
will have more time to think about numeric algorithms and how to advance their
respective applied disciplines.

1.1.2 The DMelt Software Platform

Numerical and statistical calculations explained in this book use the DataMelt
(shorter, DMelt) software platform [1] that runs on the Java platform. It is a collec-
tion of libraries integrated with different programming languages. Unlike other sta-
tistical programs, it is not limited to a single programming language: DMelt can be
used with several scripting languages such as Python/Jython, Groovy, Ruby/JRuby,
as well as with Java. Generally, the DMelt computational platform extends the stan-
dard Java software platform in several areas:
• Adds a support for Jython, Groovy, JRuby, BeanShell, and GNU Octave high-
level scripting languages.
• Adds an IDE and interactive shells to work with these scripting languages and
with Java. It also adds a support to process programs in the command line (i.e., in
a batch mode).
• Adds comprehensive Java libraries for numeric computation and visualization,
incorporating free scientific packages from more than a hundred Java developers
around the world. At the moment when this book is written, DMelt includes more
than 30,000 Java classes from more than a hundred open-source Java libraries.
• DMelt includes online resources for library updates, class documentation, and for
example databases. The Web-based package descriptions are directly accessible
from the DMelt IDE. We will discuss this topic later.
Figure 1.1 illustrates the DMelt program structure. DMelt includes a support for
several scripting languages that can be run on JVM, third-party numerical libraries
integrated with IDE, and online services for update and documentation.
DMelt was designed to enable researches to spend their time thinking about
problems and their solutions, rather than diving into low-level coding using pro-
gramming languages. DMelt analysis macros for data manipulations are based on
Jython, an implementation of the high-level language Python. Thus, one can fully
benefit from a variety of programming possibilities offered by Python, including its
syntax clarity and high-level libraries. But Jython is not a prerequisite for this frame-
work: Java and other languages supported by DMelt can also be used to access the
mathematical and graphical libraries of DMelt.
4 1 Java Computational Platform

Fig. 1.1 The structure of


the DMelt program. It
includes Java-based DMelt
API, third-party numerical
libraries, DMelt IDE, and
Web services for
documentation and updates.
This plot was created using a
DMelt Jython script shown
in Listing 8.11 of Sect. 8.6.1

With time, any computational framework based on a simple-to-learn program-


ming language naturally gets large and difficult to handle; this is quite an inevitable
feature of modern life. Properly chosen computation language is essential to main-
tain simplicity of user communication with exponentially growing programs. This
is where Java comes to its power: Java virtual machine and various integrated devel-
opment environments (Eclipse, NetBeans, IntelliJ, and other IDE) can help develop
programing codes, tell about errors or mistyped classes, and in general, provide a
layer of intelligent activity between a human, who writes a code or interprets its
algorithmic logic, and a machine designed for program execution. This is rather
different from low-level languages like C/C++ or FORTRAN which are often used
for numerical calculations. For such languages, a researcher is usually on his own
with a text editor and a programming language itself which typically requires good
programming skills and several manuals on a bookshelf.
DMelt is by no means a simple framework, although it is based on Java and
high-level Python language. As mentioned before, it has tens of thousands Java
classes and methods designed for scientific computation, data analysis, and data
visualization. The DMelt library core for statistical and graphical analysis is based
on the jhplot package, which contains more than a thousand Java classes and
methods. However, you will be surprised to find how easy it is to work with this
program. Partially, this is because of Python language implemented in Java (Jython)
and, partially, because of Java itself.
DMelt [1] is a successor of the jHepWork program that has its origin in high-
energy physics in the middle of 2000. The “Hep” part of jHepWork abbreviates
“High-Energy Physics.” The jHepWork program was described in the book [2].
DMelt substantially extends jHepWork in many areas, but remains backward com-
patible with jHepWork.

1.1.3 Some Warnings

We should immediately warn you: the DMelt numerical and graphical libraries can
be considered neither as most efficient nor error-free. The code of DMelt does not
always follow the coding recommendations for Java developers including naming
1.1 Introduction 5

conventions and code layout. We even admit that some parts were not designed
with the highest possible performance for code execution in mind. The reason is
simple: it was not written by professional programmers. The numerical libraries
were written by many people at different times, most of them were students and
scientists who had to develop numerical and data visualization algorithms for their
own research programs, since commercial software companies either could not offer
similar programs or their products were too expensive. Many contributed packages
have been discontinued many years ago, but have been brought to life after their
inclusion into DMelt. In addition, some packages were written using Java 1.1, and
this had also some impact on the coding style of certain libraries.
Thus, a professional programmer may immediately find some parts of the code
that look unprofessionally written. This is true even for some examples shown in
this book. The reason for this was not because we were not aware of such coding
issues. In some cases, we did not find appealing reasons to keep very strict coding
standard at the expense of simplicity. For example, in most cases, we import all
classes inside a package using the statement:

>>> from PackageName import *

instead of importing only certain classes as

>>> from PackageName import class1,class2

We did not enforce the latter case to keep the examples of this book short and con-
cise, so we could fit the code snippets into the pages of this book. Also, it is possible
that you may not like to type long lists of imported classes during a code prototyp-
ing (personally, I do not like this style), since this can be done later during code
deployment.
A professorial programmer might find some other odds, like why some object
containers are designed to store only double values (like the P1D class to be
discussed below), while it is more practical to store integer values when necessary.
Again, the motivation was not because of omissions. The reason was that the reader
may not want to dive into extra complexity of dealing with different types, since
integers are only a subset of float values. There are plenty of other classes which are
well suited for storing integer values (we will discuss them in this book).
The main motivation for the DMelt project was to develop an accessible and
friendly tool to be used in scientific search, with a syntax oriented toward scientists
rather than programmers. The design of this project was mainly motivated by
simplicity: there are many programming languages which are required to learn for
many years before starting to write useful scientific and engineering projects. The
approach discussed in this book is very different: generally, the reader does not need
to know any programming language to start writing analysis codes using DMelt
libraries. However, if it happens that the reader knows either Java or Python (or
both) already, he or she will find this book to be also interesting, since DMelt is not
just a simplified entry to the world of the Java and Python computer programming. It
6 1 Java Computational Platform

shows how to use programming for practical purposes such as numeric calculations,
statistics, and data analysis.
The reader may also notice that a little attention has been paid to how to write and
use Java or Jython classes. Of course, classes are necessary for any object-oriented
language. The reason for this is the following: for the majority of scientific data
analysis programs, the logic of scripting programs is linear, i.e., an analysis code
typically consists of a well-defined sequence of statements to be evaluated one by
one, from the top to the bottom of the code. It is very unlikely that data analysis logic
will contain highly parallel algorithmic branches as those for the usual graphical
user interface (GUI) development.1 Certainly, the classes are necessary when one
develops Java libraries to be used by a scripting language. But, in this book, we
mainly concentrate on the scripting examples based on the existing Java libraries of
DMelt, rather than discussing how to write classes for numerical computation to be
deployed as external libraries.

1.1.4 Errors

This book may contain typos, omissions, or even errors. DMelt can also contain
bugs. If you notice any errors or if you have suggestions regarding the book and
code examples, I would be happy to hear from you. You can send your comments
to:

[email protected]

One can also post bug reports to the DMelt forum accessible from the main Web
page:

http://jwork.org/dmelt/

DMelt is not a software that stands still. Therefore, this book represents a snap-
shot of the time when the DMelt version 1.4 was in use, therefore, some examples
may fall out of date. Therefore, the reader is encouraged to look at the Web page
given above to find corrected examples.

1 We should probably say that this may not be totally true in future when multi-core machines will
be rather common and one will face with the question of how to parallelize analysis codes to gain
high performance. We briefly discuss this topic in this book.
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Curly: A Tale
of the Arizona Desert
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: Curly: A Tale of the Arizona Desert

Author: Roger Pocock

Release date: November 23, 2012 [eBook #41447]


Most recently updated: October 23, 2024

Language: English

Credits: Produced by D Alexander, Mary Meehan and the Online


Distributed Proofreading Team at http://www.pgdp.net
(This
file was produced from images generously made
available
by The Internet Archive)

*** START OF THE PROJECT GUTENBERG EBOOK CURLY: A TALE


OF THE ARIZONA DESERT ***
CURLY
A TALE OF THE ARIZONA DESERT
By ROGER POCOCK
Author of "A Frontiersman," etc.
Boston
Little, Brown, and Company
Copyright, 1904,
By Roger Pocock.
Copyright, 1905,
By Little, Brown, and Company.
All rights reserved.
Published May, 1905.
Printers
S. J. Parkhill & Co., Boston, U.S.A.
CONTENTS
CHAPTER PAGE
I. Apaches 1
II. Lord Balshannon 9
III. Holy Cross 16
IV. The Range Wolves 27
V. Back to the Wolf Pack 37
VI. My Range Whelps Whimpering 44
VII. At the Sign of Ryan's Hand 52
VIII. In the Name of the People 65
IX. War Signs 69
X. Storm Gathering 78
XI. The Gun-fight 89
XII. The City Boiling Over 106
XIII. The Man-hunt 118
XIV. The Frontier Guards 126
XV. Mostly Chalkeye 138
XVI. Arranging for more Trouble 145
XVII. The Real Curly 156
XVIII. The White Star 167
XIX. A Marriage Settlement 184
XX. The Marshal's Posse 200
XXI. A Flying Hospital 212
XXII. Robbery-under-Arms 222
XXIII. A House of Refuge 234
XXIV. The Saving of Curly 254
XXV. A Million Dollars Ransom 272
XXVI. The Stronghold 290
XXVII. A Second-hand Angel 314
CURLY
CHAPTER I
APACHES
Back in Old Texas, 'twixt supper and sleep time, the boys in camp
would sit around the fire and tell lies. They talked about the Ocean
which was bigger than all the plains, and I began to feel worried
because I'd never seen what the world was like beyond the far edge
of the grass. Life was a failure until I could get to that Ocean to
smell and see for myself. After that I would be able to tell lies about
it when I got back home again to the cow-camps. When I was old
enough to grow a little small fur on my upper lip I loaded my pack
pony, saddled my horse, and hit the trail, butting along day after day
towards the sunset, expecting every time I climbed a ridge of hills to
see the end of the yellow grass and the whole Pacific Ocean shining
beyond, with big ships riding herd like cowboys around the grazing
whales.
One morning, somewheres near the edge of Arizona, I noticed my
horse throw his ears to a small sound away in the silence to the left.
It seemed to be the voice of a rifle, and maybe some hunter was
missing a deer in the distance, so I pointed that way to inquire. After
a mile or so I heard the rifle speaking again, and three guns
answered, sputtering quick and excited. That sounded mighty like a
disagreement, so I concluded I ought to be cautious and roll my tail
at once for foreign parts. I went on slow, approaching a small hill.
Again a rifle-shot rang out from just beyond the hill, and two shots
answered—muzzle-loading guns. At the same time the wind blew
fresh from the hill, with a whiff of powder, and something else which
made my horses shy. "Heap bad smell!" they snuffed. "Just look at
that!" they signalled with their ears. "Ugh!" they snorted.
"Get up!" said I; and charged the slope of the hill.
Near the top I told them to be good or I'd treat them worse than a
tiger. Then I went on afoot with my rifle, crept up to the brow of the
hill, and looked over through a clump of cactus.
At the foot of the hill, two hundred feet below me, there was
standing water—a muddy pool perhaps half an acre wide—and just
beyond that on the plain a burned-out camp fire beside a couple of
canvas-covered waggons. It looked as if the white men there had
just been pulling out of camp, with their teams all harnessed for the
trail, for the horses lay, some dead, some wounded, mixed up in a
struggling heap. As I watched, a rifle-shot rang out from the
waggons, aimed at the hillside, but when I looked right down I could
see nothing but loose rocks scattered below the slope. After I
watched a moment a brown rock moved; I caught the shine of an
Indian's hide, the gleam of a gun-barrel. Close by was another
Indian painted for war, and beyond him a third lying dead. So I
counted from rock to rock until I made out sixteen of the worst kind
of Indians—Apaches—all edging away from cover to cover to the
left, while out of the waggons two rifles talked whenever they saw
something to hit. One rifle was slow and cool, the other scared and
panicky, but neither was getting much meat.
For a time I reckoned, sizing up the whole proposition. While the
Apaches down below attacked the waggons, their sentry up here on
the hill had forgotten to keep a look-out, being too much interested.
He'd never turned until he heard my horses clattering up the rocks,
but then he had yelled a warning to his crowd and bolted. One
Indian had tried to climb the hill against me and been killed from the
waggons, so now the rest were scared of being shot from above
before they could reach their ponies. They were sneaking off to the
left in search of them. Off a hundred yards to the left was the sentry,
a boy with a bow and arrows, running for all he was worth across
the plain. A hundred yards beyond him, down a hollow, was a
mounted Indian coming up with a bunch of ponies. If the main body
of the Apaches got to their ponies, they could surround the hill,
charge, and gather in my scalp. I did not want them to take so much
trouble with me.
Of course, my first move was to up and bolt along the ridge to the
left until I gained the shoulder of the hill. There I took cover, and
said, "Abide with me, and keep me cool, if You please!" while I
sighted, took a steady bead, and let fly at the mounted Indian. At
my third shot he came down flop on his pony's neck, and that was
my first meat. The bunch of ponies smelt his blood and stampeded
promiscuous.
The Apaches, being left afoot, couldn't attack me none. If they tried
to stampede they would be shot from the waggons, while I hovered
above their line of retreat considerably; and if they stayed I could
add up their scalps like a sum in arithmetic. They were plumb
surprised at me, and some discouraged, for they knew they were
going to have disagreeable times. Their chief rose up to howl, and a
shot from the waggons lifted him clean off his feet. It was getting
very awkward for those poor barbarians, and one of them hoisted a
rag on his gun by way of surrender.
Surrender? This Indian play was robbery and murder, and not the
honest game of war. The man who happens imprudent into his own
bear-trap is not going to get much solace by claiming to be a warrior
and putting up white flags. The game was bear-traps, and those
Apaches had got to play bear-traps now, whether they liked it or
not. There were only two white folks left in the waggons, and one
on the hill, so what use had we for a dozen prisoners who would lie
low till we gave them a chance, then murder us prompt. The man
who reared up with the peace flag got a shot from the waggons
which gave him peace eternal.
Then I closed down with my rifle, taking the Indians by turns as they
tried to bolt, while the quiet gun in the waggon camp arrested
fugitives and the scary marksman splashed lead at the hill most
generous. Out of sixteen Apaches two and the boy got away intact,
three damaged, and the rest were gathered to their fathers.
When it was all over I felt unusual solemn, running my paw slow
over my head to make sure I still had my scalp; then collected my
two ponies and rode around to the camp. There I ranged up with a
yell, lifting my hand to make the sign of peace, and a man came
limping out from the waggons. He carried his rifle, and led a yearling
son by the paw.
The man was tall, clean-built, and of good stock for certain, but his
clothes were in the lo-and-behold style—a pane of glass on the off
eye, stand-up collar, spotty necktie, boiled shirt, riding-breeches with
puffed sleeves most amazing, and the legs of his boots stiff like a
brace of stove-pipes. His near leg was all bloody and tied up with a
tourniquet bandage. As to his boy Jim, that was just the quaintest
thing in the way of pups I ever saw loose on the stock range. He
was knee-high to a dawg, but trailed his gun like a man, and looked
as wide awake as a little fox. I wondered if I could tame him for a
pet.
"How d'ye do?" squeaked the pup, as I stepped down from the
saddle.
I allowed I was feeling good.
"I'm sure," said the man, "that we're obliged to you and your friends
on the hill. In fact, very much obliged."
Back in Texas I'd seen water go to sleep with the cold, but this man
was cool enough to freeze a boiler.
"Will you—er—ask your friends," he drawled, "to come down? I'd like
to thank them."
"I'll pass the glad word," said I. "My friends is in Texas."
"My deah fellow, you don't—aw—mean to say you were alone?"
"Injuns can shoot," said I, "but they cayn't hit."
"Two of my men are dead and the third is dying. I defer to your—er
—experience, but I thought they could—er—hit."
Then I began to reckon I'd been some hazardous in my actions. It
made me sweat to think.
"Well," said I, to be civil, "I cal'late I'd best introduce myself to you-
all. My name's Davies."
"I'm Lord Balshannon," said he, mighty polite.
"And I'm the Honourable Jim du Chesnay," squeaked the kid.
I took his paw and said I was proud to know a warrior with such
heap big names. The man laughed.
"Wall, Mister Balshannon," says I, "your horses is remnants, and the
near fore wheel of that waggon is sprung to bust, and them Apaches
has chipped your laig, which it's broke out bleeding again, so I
reckon——"
"You have an eye for detail," he says, laughing; "but if you will
excuse me now, I'm rather busy."
He looked into my eyes cool and smiling, asking for no help, ready
to rely on himself if I wanted to go. A lump came into my throat, for
I sure loved that man from the beginning.
"Mr. Balshannon," says I, "put this kid on top of a waggon to watch
for Indians, while you dress that wound. I'm off."
He turned his back on me and walked away.
"I'll be back," said I, busy unloading my pack-horse. "I'll be back," I
called after him, "when I bring help!"
At that he swung sudden and came up against me. "Er—thanks," he
said, and grabbed my paw. "I'm awfully obliged, don't you know."
I swung to my saddle and loped off for help.
CHAPTER II
LORD BALSHANNON
With all the signs and the signal smokes pointing for war, I reckoned
I could dispense with that Ocean and stay round to see the play.
Moreover, there was this British lord, lost in the desert, wounded
some, helpless as a baby, game as a grizzly bear, ringed round with
dead horses and dead Apaches, and his troubles appealed to me
plentiful. I scouted around until I hit a live trail, then streaked away
to find people. I was doubtful if I had done right in case that lord
got massacred, me being absent, so I rode hard, and at noon saw
the smoke of a camp against the Tres Hermanos Mountains. It
proved to be a cow camp with all the boys at dinner.
They had heard nothing of Apaches out on the war trail, but when I
told what I knew, they came glad, on the dead run, their waggons
and pony herd following. We found the Britisher digging graves for
three dead men, and looking apt to require a fourth for his own use.
"Er—good evening," says he, and I began to wonder why I'd
sweated myself so hot to rescue an iceberg.
"Gentlemen," says he to the boys, "you find some er—coffee ready
beside the fire, and afterwards, if you please, we will bury my dead."
The boys leaned over in their saddles, wondering at him, but the
lord's cool eye looked from face to face, and we had to do what he
said. He was surely a great chief, that Lord Balshannon.
The men who had fallen a prey to the Apaches were two teamsters
and a Mexican, all known to these Bar Y riders, and they were sure
sorry. But more than that they enjoyed this shorthorn, this
tenderfoot from the east who could stand off an outfit of hostile
Indians with his lone rifle. They saw he was wounded, yet he dug
graves for his dead, made coffee for the living, and thought of
everything except himself. After coffee we lined up by the graves to
watch the bluff he made at funeral honours. Lord Balshannon was a
colonel in the British Army, and he stood like an officer on parade
reading from a book. His black hair was touched silver, his face was
strong, hard, manful, and his voice quivered while he read from the
little book—

"For I am a stranger with Thee,


And a sojourner as all my fathers were;
O spare me a little, that I may recover my strength
Before I go hence, and am no more seen."

I reckon that there were some of us sniffing as though we had just


caught a cold, while we listened to that man's voice, and saw the
loneliness of him. Afterwards Dick Bryant, the Bar Y foreman, walked
straight up to Balshannon.
"Britisher," said he, "you may be a sojourner, and we hopes you are,
a whole lot, but there's no need to be a stranger. Shake."
So they shook hands, and that was the beginning of a big friendship.
Then Balshannon turned to the crowd, and looked slowly from face
to face of us.
"Gentlemen," he said kind of feeble, and we saw his face go grey
while he spoke, "I'm much obliged to you all for er—for coming. It
seems, indeed, ah—that my little son Jim and I have made friends
and er—neighbours. I'm sorry that you should find my camp in such
aw—in such a beastly mess, but there's some fairly decent whisky in
this nearest waggon, and er—" the man was reeling, and his eyes
seemed blind, "when we get to my new ranche at Holy Cross I—I
hope you'll—friends—aw—and——"
And he dropped in a dead faint.
So long as I stay alive I shall remember that night, the smell of the
dead horses, the silence, the smoke of our fire going up straight to a
white sky of stars, the Bar Y people in pairs lying wrapped in their
blankets around the waggons, the reliefs of riders going out on
guard, the cold towards dawn. The little boy Jim had curled up
beside me because he felt lonesome in the waggon. Balshannon lay
by the fire, his mind straying away off beyond our range. Often he
muttered, but I could not catch the words, and sometimes said
something aloud which sounded like nonsense. It must have been
midnight, when all of a sudden he sat bolt upright, calling out loud
enough to waken half the camp—
"Ryan!" he shouted, "don't disturb him, Ryan! He's upstairs dying. If
you fire, the shock will—Ryan! Don't shoot! Ryan!"
Then with a groan he fell back. I moistened his lips with cold tea.
"All right," he whispered, "thanks, Helen."
For a long time he lay muttering while I held his hands. "You see,
Helen," he whispered, "neither you nor the child could be safe in
Ireland. Ryan killed my father."
He seemed to fall asleep after that, and, counting by the stars, an
hour went by. Then he looked straight at me—
"You see, dear? I turned them out of their farms, and Ryan wants
his revenge, so——"
Towards morning I put some sticks on the fire which crackled a lot.
"Go easy, Jim," I heard him say, "don't waste our cartridges. Poor
little chap!"
Day broke at last, the cook was astir, and the men rode in from
herd. I dropped off to sleep.
It was noon before the heat awakened me, and I sat up to find the
fire still burning, but Lord Balshannon gone. I saw his waggons
trailing off across the desert. Dick Bryant was at the fire lighting his
pipe with a coal.
"Wall," said he, "you've been letting out enough sleep through yo'
nose to run an engine. Goin' to make this yo' home?"
"The camp's moved?"
"Sure. I've sent the Britisher's waggons down to Holy Cross. He
bought the place from a Mexican last month."
"Is it far?"
"About twenty mile. I've been down there this morning. I reckon the
people there had smelt Apaches and run. It was empty, and that's
why I'm making this talk to you. I cayn't spare my men after to-day,
and I don't calculate to leave a sick man and a lil' boy thar alone."
"I'll stay with them," said I.
"That's good talk. If you-all need help by day make a big smoke on
the roof, or if it's night just make a flare of fire. I'll keep my outfit
near enough to see."
"You reckon there'll be Indians?"
"None. That was a stray band, and what's left of it ain't feeling good
enough to want scalps. But when I got to Holy Cross this morning I
seen this paper, and some tracks of the man who left it nailed on the
door. I said nothing to my boys, and the Britisher has worries
enough already to keep him interested, but you ought to know
what's coming, in case of trouble. Here's the paper.

"'Grave City, Arizona,


"'3rd February,
1886.
"'My Lord,
"'This is to tell you that in spite of everything you could do to
destroy me, I'm safe in this free country, and doing well. I've
heard of the horrible crime you committed in driving the poor
people from your estate in Ireland, from homes which we and
our fathers have loved for a thousand years. Now I call the holy
saints to witness that I will do to you as you have done to me,
and to my people. The time will come when, driven from this
your new home, without a roof to cover you, or a crust to eat,
your wife and boy turned out to die in the desert, you will plead
for even so much as a drink, and it will be thrown in your face. I
shall not die until I have seen the end of your accursed house.
"'(Sd.) George
Ryan.'

"These Britishers," said Bryant, "is mostly of two breeds—the lords


and the flunkeys; and you kin judge them by the ways they act. This
Mr. Balshannon is a lord, and thish yer Ryan's a flunk. If a real man
feels that his enemy is some superfluous on this earth, he don't
make lamentations and post 'em up on a door. No, he tracks his
enemy to a meeting; he makes his declaration of war, and when the
other gentleman is good and ready, they lets loose with their guns in
battle. This Ryan here has the morals of a snake and the right hand
of a coward."
"Do I give this paper," said I, "to Mr. Balshannon?"
"It's his business, lad, not ours. But until this lord is well enough to
fight, you stands on guard."
CHAPTER III
HOLY CROSS
Editor's Note.—The walls of Holy Cross rise stark from the top of
a hill on the naked desert; and in all the enormous length and
breadth of this old fortress there is no door or window to invite
attack. At each of the four corners stands a bastion tower to
command the flanks, and in the north wall low towers defend
the entrance, which is a tunnel through the buildings, barred by
massive doors, and commanded by loopholes for riflemen. The
house is built of sun-dried bricks, the ceilings of heavy beams
supporting a flat roof of earth.
As one enters the first courtyard one sees that the buildings on
the right are divided up into a number of little houses for the
riders and their families; in front is the gate of the stable court,
on the left are the chapel and the dining-hall, and in the middle
of the square there is a well. Through the dining-hall on the left
one enters the little court with its pool covered with water-lilies,
shaded by palm trees, and surrounded by an arcade which is
covered by creeping plants, ablaze with flowers. The private
rooms open upon this cloister, big, cool, and dark, forming a
little palace within the fortress walls. Such is the old Hacienda
Santa Cruz which Lord Balshannon had bought from El Señor
Don Luis Barrios.

From the beginning I saw no sign and smelt no whiff of danger


either of Apaches or of Mr. Ryan. When Balshannon was able to ride
I gave him Ryan's letter, watched him read it quietly, but got nary
word from him. He looked up from the letter, smiling at my glum
face.
"Chalkeye," said he, "couldn't we snare a rabbit for Jim to play
with?" He and the kid and me used to play together like babies, and
Jim was surely serious with us men for being too young.
In those days Balshannon took advice from Bryant, our nearest
neighbour, whose ranche was only one day's ride from Holy Cross.
Dick helped him to buy good cattle to stock our range, and two
thoroughbred English bulls to improve the breed. Then he bought
ponies, and hired Mexican riders. So I began to tell my boss and his
little son about cows and ponies—the range-riding, driving, and
holding of stock; the roping, branding, and cutting out; how to judge
grass, to find water, to track, scout, and get meat for the camp. The
boss was too old and set in his ways to learn new play, but Jim had
his heart in the business from the first, growing up to cow-punching
as though he were born on the range.
Besides that I had to learn them both the natural history of us
cowboys, the which is surprising to strangers, and some prickly.
Being thoroughbred stock, this British lord and his son didn't need to
put on side, or make themselves out to be better than common folks
like me.
After the first year, when things were settled down and the weather
cool, Lady Balshannon came to Holy Cross, and lived in the garden
court under the palm trees. She was a poor invalid lady, enjoying
very bad health, specially when we had visitors or any noise in the
house. She never could stand up straight against the heat of the
desert. On the range I was teacher to Jim; but in the house this lady
made the kid and me come to school for education. We used to race
neck and neck over our sums and grammar of an evening. I guess I
was the most willing, but the kid had much the best brains. He beat
me anyways.
Sometimes I got restless, sniffing up wind for trouble, riding around
crazy all night because I was too peaceful and dull to need any
sleep. But then the boss wanted me in his business, the lady needed
me for lessons and to do odd jobs, the kid needed me to play with
and to teach him the life of the stock range; so when I got "Pacific
Ocean fever" they all made such a howl that I had to stay. Stopping
at Holy Cross grew from a taste into a habit, and you only know the
strength of a habit when you try to kill it. That family had a string
round my hind leg which ain't broken yet.
The boss made me foreman over his Mexican cowboys, and major-
domo in charge of Holy Cross. In the house I was treated like a son,
with my own quarters, servants, and horses, and my wages were
paid to me in ponies until there were three hundred head marked
with my private brand. Some people with bad hearts and forked
tongues have claimed that I stole these horses over in Mexico. I
treat such with dignified silence and make no comment except to
remark that they are liars. Anyway, as the years rolled on, and the
business grew, Mr. Chalkeye Davies became a big chief on the range
in Arizona.
When the kid was fourteen years old he quit working cows with me,
and went to college. Balshannon missed him some, for he took to
straying then, and would go off in the fall of the year for a bear-
hunt, in the winter to stay with friends, and the rest of the time
would hang around Grave City. I reckon the desert air made him
thirsty, because he drank more than was wise, and the need for
excitement set him playing cards, so that he lost a pile of money
bucking against the faro game and monte. He left me in charge of
his business, to round up his calves for branding, and his beef for
sale, to keep the accounts, to pay myself and my riders, and ride
guard for his lady while she prayed for his soul, alone at Holy Cross.
When Jim wanted money at college he wrote to me. In all that time
we were not attacked by Indians, Ryans, or any other vermin.
Upon the level roof of Holy Cross there was space enough to handle
cavalry, and a wide outlook across the desert. There we had lie-
down chairs, rugs, and cushions; and after dinner, when the day's
work was done, we would sit watching the sunset, the red afterglow,
the rich of night come up in the east, the big stars wheeling slowly
until it was sleep-time. But when the boy was at college, and the
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like