summaryrefslogtreecommitdiff
path: root/training/liblbfgs/arithmetic_ansi.h
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-05-08 15:19:40 -0400
committerChris Dyer <cdyer@cs.cmu.edu>2012-05-08 15:19:40 -0400
commite66b487c6bf4debb1326ad8c7169a44badace365 (patch)
tree1ad89e58b6df5a3632a0b4e95c6014574be273bd /training/liblbfgs/arithmetic_ansi.h
parenta3d0e4f8c8f669fd4c0dac7a193a12148253d4e2 (diff)
add liblbfgs, which is much less crappy than the current one
Diffstat (limited to 'training/liblbfgs/arithmetic_ansi.h')
-rw-r--r--training/liblbfgs/arithmetic_ansi.h133
1 files changed, 133 insertions, 0 deletions
diff --git a/training/liblbfgs/arithmetic_ansi.h b/training/liblbfgs/arithmetic_ansi.h
new file mode 100644
index 00000000..fa390da3
--- /dev/null
+++ b/training/liblbfgs/arithmetic_ansi.h
@@ -0,0 +1,133 @@
+/*
+ * ANSI C implementation of vector operations.
+ *
+ * Copyright (c) 2007-2010 Naoaki Okazaki
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/* $Id$ */
+
+#include <stdlib.h>
+#include <memory.h>
+
+#if LBFGS_FLOAT == 32 && LBFGS_IEEE_FLOAT
+#define fsigndiff(x, y) (((*(uint32_t*)(x)) ^ (*(uint32_t*)(y))) & 0x80000000U)
+#else
+#define fsigndiff(x, y) (*(x) * (*(y) / fabs(*(y))) < 0.)
+#endif/*LBFGS_IEEE_FLOAT*/
+
+inline static void* vecalloc(size_t size)
+{
+ void *memblock = malloc(size);
+ if (memblock) {
+ memset(memblock, 0, size);
+ }
+ return memblock;
+}
+
+inline static void vecfree(void *memblock)
+{
+ free(memblock);
+}
+
+inline static void vecset(lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ x[i] = c;
+ }
+}
+
+inline static void veccpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ y[i] = x[i];
+ }
+}
+
+inline static void vecncpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ y[i] = -x[i];
+ }
+}
+
+inline static void vecadd(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ y[i] += c * x[i];
+ }
+}
+
+inline static void vecdiff(lbfgsfloatval_t *z, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ z[i] = x[i] - y[i];
+ }
+}
+
+inline static void vecscale(lbfgsfloatval_t *y, const lbfgsfloatval_t c, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ y[i] *= c;
+ }
+}
+
+inline static void vecmul(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
+{
+ int i;
+
+ for (i = 0;i < n;++i) {
+ y[i] *= x[i];
+ }
+}
+
+inline static void vecdot(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n)
+{
+ int i;
+ *s = 0.;
+ for (i = 0;i < n;++i) {
+ *s += x[i] * y[i];
+ }
+}
+
+inline static void vec2norm(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n)
+{
+ vecdot(s, x, x, n);
+ *s = (lbfgsfloatval_t)sqrt(*s);
+}
+
+inline static void vec2norminv(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n)
+{
+ vec2norm(s, x, n);
+ *s = (lbfgsfloatval_t)(1.0 / *s);
+}