From 068880acd770a8c753adc038f1762f4624123fec Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Thu, 8 Oct 2015 17:48:22 +0200
Subject: [PATCH] - repair msgs from fit - allow z<n>[,i]

---
 pub/lib/expr.cpp    |  9 +++++----
 pub/lib/fit.cpp     | 20 ++++++++++----------
 pub/lib/node.cpp    | 22 ++++++++++++++++++----
 pub/test/i_as_j.f2t |  9 +++++++++
 4 files changed, 42 insertions(+), 18 deletions(-)
 create mode 100755 pub/test/i_as_j.f2t

diff --git a/pub/lib/expr.cpp b/pub/lib/expr.cpp
index cc1cd72b..e2d64f15 100644
--- a/pub/lib/expr.cpp
+++ b/pub/lib/expr.cpp
@@ -9,6 +9,7 @@
 
 #include "defs.hpp"
 
+#include <cassert>
 #include <algorithm>
 
 #include "../trivia/vector_ops.hpp"
@@ -137,10 +138,10 @@ int CRef::get_j( const CContext& ctx, int nj ) const
     int j;
     if ( tj ) {
         RObj tmp = tj->tree_val( ctx );
-        RObjInt pi = PCAST<const CObjInt>(tmp);
-        if ( !pi )
-            throw "index J=" + tmp->to_s() + " is not integer";
-        j = pi->val;
+        if ( RObjInt pi = PCAST<const CObjInt>(tmp) )
+            j = pi->val;
+        else
+            throw "index J {" + tj->tree_info() + "}->{" + tmp->to_s() + "} is of unexpected type";
     } else {
         j = ctx.j;
     }
diff --git a/pub/lib/fit.cpp b/pub/lib/fit.cpp
index 480ed1a2..c93f4491 100644
--- a/pub/lib/fit.cpp
+++ b/pub/lib/fit.cpp
@@ -498,20 +498,20 @@ void NCurveFit::fit( bool _allow_slow_conv )
             fit_global( fc, fd, fiter.k(), control );
         } else {
             fc->chi2 = 0;
-            int jout=0; // to be reported on
-            vector<string> out(fc->nJ(), "");
+            vector<int> J;
+            for ( int j=0; j<fc->nJ(); j++ )
+                if ( !fc->VC(j)->frozen )
+                    J.push_back( j );
+            int jout=0; // to report on
+            vector<string> out(J.size(), "");
 #pragma omp parallel for if ( !verbosity )
-            for ( int j=0; j<fc->nJ(); j++ ) {
-                if ( fc->VC(j)->frozen )
-                    continue;
-                out[j] = fit_one_spec( fc, fd, fiter.k(), j, control ) + "\n";
+            for ( int jj=0; jj<J.size(); ++jj ) {
+                out[J[jj]] = fit_one_spec( fc, fd, fiter.k(), J[jj], control ) + "\n";
 #pragma omp critical
                 { // report results in regular order
-                    if ( j==jout ) {
-                        cout << out[jout++];
-                        while( jout<fc->nJ() && out[jout]!="" )
+                    if ( J[jj]==jout )
+                        while( jout<J.size() && out[jout]!="" )
                             cout << out[jout++];
-                    }
                 }
             } // j
         } // fit mode
diff --git a/pub/lib/node.cpp b/pub/lib/node.cpp
index 4a520595..4d7f2e46 100644
--- a/pub/lib/node.cpp
+++ b/pub/lib/node.cpp
@@ -720,8 +720,8 @@ void CNodeIva::npar_exec( int *np ) const
 const RObj CNodeIva::tree_val( const CContext& ctx ) const
 {
     try {
-        if ( (var->typ == CVar::_I || var->typ == CVar::_J ||
-              var->typ == CVar::_K) && ( ref->ti || ref->tj || ref->tk ) )
+        if ( (var->typ == CVar::_I || var->typ == CVar::_J || var->typ == CVar::_K) &&
+             ( ref->ti || ref->tj || ref->tk ) )
             throw S("invalid cross reference: index cannot be indexed");
 
         // Get k from context:
@@ -755,8 +755,22 @@ const RObj CNodeIva::tree_val( const CContext& ctx ) const
                 throw S("Z does not require 3 indices");
             if ( var->num>= f->nZ() )
                 throw "invalid reference " + var->var_info();
-            int j = ref->get_j( ctx, f->nJ() );
-            return f->V[j]->z[var->num];
+            if        ( ctx.dim==CContext::_1 ) {
+                int j = ref->get_j( ctx, f->nJ() );
+                return f->V[j]->z[var->num];
+            } else if  ( ctx.dim==CContext::_VI ) {
+                CContext myctx = ctx;
+                myctx.dim = CContext::_1;
+                PObjVecEnu pret( new CObjVecEnu( ctx.nv ) );
+                for ( int ii=0; ii<ctx.nv; ++ii ) {
+                    myctx.i = ii;
+                    int j = ref->get_j( myctx, f->nJ() );
+                    pret->v[ii]  = f->V[j]->z[var->num]->to_r();
+                    pret->dv[ii] = f->V[j]->z[var->num]->to_dr();
+                }
+                return pret;
+            } else
+                throw S("BUG: unexpected context");
         }
 
         // References for data file
diff --git a/pub/test/i_as_j.f2t b/pub/test/i_as_j.f2t
new file mode 100755
index 00000000..55de5f1e
--- /dev/null
+++ b/pub/test/i_as_j.f2t
@@ -0,0 +1,9 @@
+#!/usr/bin/env frida
+fm 4 3 h
+oy! 10*j+i
+oz0! 100*j
+0 oi y[,,1]
+1 oy z0[0,i]
+throw_unless(x[2,0,1]==100,"x_wrong")
+throw_unless(y[2,0,1]==100,"y_wrong")
+exit(1)
\ No newline at end of file
-- 
GitLab