<commit_before>/** @file gsXml.hpp

    @brief Provides implementation of XML helper functions.

    This file is part of the G+Smo library. 

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
    
    Author(s): <NAME>
*/

#include <sstream>
#include <gsCore/gsLinearAlgebra.h>
#include <gsCore/gsFunctionExpr.h>

namespace gismo {

namespace internal {

/*
template<class Object>
std::string gsXml<Object>::tag ()
{
    // Next line will produce compile-time error
    // if gsXml is not specialized for Object
    Object::Object_does_not_exist_ERROR;
    return "";
}
*/

template<class T>
gsXmlNode * makeNode( const std::string & name, 
                      const gsMatrix<T> & value, gsXmlTree & data,
                      bool transposed)
{
    std::ostringstream oss;
    // Set precision
    oss << std::setprecision(FILE_PRECISION);
  
    if ( transposed )
        for ( index_t j = 0; j< value.rows(); ++j)
        {
            for ( index_t i = 0; i< value.cols(); ++i)
                oss << value(j,i) <<" ";
            //oss << "\n";
        }
    else
        for ( index_t j = 0; j< value.cols(); ++j)
        {
            for ( index_t i = 0; i< value.rows(); ++i)
                oss << value(i,j) <<" ";
            //oss << "\n";
        }
  
    return makeNode(name, oss.str(), data);
}

template<class T>
void getFunctionFromXml ( gsXmlNode * node, gsFunctionExpr<T> & result ) 
{
    //gsWarn<<"Reading "<< node->name() <<" function\n";

    GISMO_ASSERT( node->first_attribute("dim"), "getFunctionFromXml: No dim found" ) ;
    const int d = atoi( node->first_attribute("dim")->value() );
  
    std::vector< std::string > expr_strings;

    gsXmlNode * child = node->first_node("c");

    if (child != NULL )
    {
        for (; child; child = child->next_sibling() )
            expr_strings.push_back(  child->value() );
    }
    else
        expr_strings.push_back(  node->value() );

    result = gsFunctionExpr<T>( expr_strings, d );
}

template<class T>
void getMatrixFromXml ( gsXmlNode * node, unsigned const & rows, 
                        unsigned const & cols, gsMatrix<T> & result ) 
{
    //gsWarn<<"Reading "<< node->name() <<" matrix of size "<<rows<<"x"<<cols<<"Geometry..\n";
    std::istringstream str;
    str.str( node->value() );
    result.resize(rows,cols);
 
    for (unsigned i=0; i<rows; ++i)
        for (unsigned j=0; j<cols; ++j)
            //if ( !(str >> result(i,j) ) )
              if (! gsGetValue(str,result(i,j)) )
            {
                gsWarn<<"XML Warning: Reading matrix of size "<<rows<<"x"<<cols<<" failed.\n";
                gsWarn<<"Tag: "<< node->name() <<", Matrix entry: ("<<i<<", "<<j<<").\n";
                return;
            }
}

template<class T>
void getSparseEntriesFromXml ( gsXmlNode * node,
                              gsSparseEntries<T> & result ) 
{
    result.clear();

    std::istringstream str;
    str.str( node->value() );
    index_t r,c;
    T val;

    while( (str >> r) && (str >> c) && (str >> val) ) 
        result.add(r,c,val);
}


template<class T>
gsXmlNode * putMatrixToXml ( gsMatrix<T> const & mat, gsXmlTree & data, std::string name) 
{
    std::ostringstream str;
    str << std::setprecision(FILE_PRECISION);
    // Write the matrix entries
    for (index_t i=0; i< mat.rows(); ++i)
    {
        for (index_t j=0; j<mat.cols(); ++j)
            str << mat(i,j)<< " ";
        str << "\n";
    }
<commit_msg>fix in matrix i/o<commit_after>/** @file gsXml.hpp

    @brief Provides implementation of XML helper functions.

    This file is part of the G+Smo library. 

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
    
    Author(s): <NAME>
*/

#include <sstream>
#include <gsCore/gsLinearAlgebra.h>
#include <gsCore/gsFunctionExpr.h>

namespace gismo {

namespace internal {

/*
template<class Object>
std::string gsXml<Object>::tag ()
{
    // Next line will produce compile-time error
    // if gsXml is not specialized for Object
    Object::Object_does_not_exist_ERROR;
    return "";
}
*/

template<class T>
gsXmlNode * makeNode( const std::string & name, 
                      const gsMatrix<T> & value, gsXmlTree & data,
                      bool transposed)
{
    std::ostringstream oss;
    // Set precision
    oss << std::setprecision(FILE_PRECISION);
  
    if ( transposed )
        for ( index_t j = 0; j< value.rows(); ++j)
        {
            for ( index_t i = 0; i< value.cols(); ++i)
                oss << value(j,i) <<" ";
            //oss << "\n";
        }
    else
        for ( index_t j = 0; j< value.cols(); ++j)
        {
            for ( index_t i = 0; i< value.rows(); ++i)
                oss << value(i,j) <<" ";
            //oss << "\n";
        }
  
    return makeNode(name, oss.str(), data);
}

template<class T>
void getFunctionFromXml ( gsXmlNode * node, gsFunctionExpr<T> & result ) 
{
    //gsWarn<<"Reading "<< node->name() <<" function\n";

    GISMO_ASSERT( node->first_attribute("dim"), "getFunctionFromXml: No dim found" ) ;
    const int d = atoi( node->first_attribute("dim")->value() );
  
    std::vector< std::string > expr_strings;

    gsXmlNode * child = node->first_node("c");

    if (child != NULL )
    {
        for (; child; child = child->next_sibling() )
            expr_strings.push_back(  child->value() );
    }
    else
        expr_strings.push_back(  node->value() );

    result = gsFunctionExpr<T>( expr_strings, d );
}

template<class T>
void getMatrixFromXml ( gsXmlNode * node, unsigned const & rows, 
                        unsigned const & cols, gsMatrix<T> & result ) 
{
    //gsWarn<<"Reading "<< node->name() <<" matrix of size "<<rows<<"x"<<cols<<"Geometry..\n";
    std::istringstream str;
    str.str( node->value() );
    result.resize(rows,cols);
 
    for (unsigned i=0; i<rows; ++i)
        for (unsigned j=0; j<cols; ++j)
            //if ( !(str >> result(i,j) ) )
              if (! gsGetValue(str,result(i,j)) )
            {
                gsWarn<<"XML Warning: Reading matrix of size "<<rows<<"x"<<cols<<" failed.\n";
                gsWarn<<"Tag: "<< node->name() <<", Matrix entry: ("<<i<<", "<<j<<").\n";
                return;
            }
}

template<class T>
void getSparseEntriesFromXml ( gsXmlNode * node,
                              gsSparseEntries<T> & result ) 
{
    result.clear();

    std::istringstream str;
    str.str( node->value() );
    index_t r,c;
    T val;

    //while( (str >> r) && (str >> c) && (str >> val) )
    while( (str >> r) && (str >> c) && ( gsGetValue(str,val)) )
        result.add(r,c,val);
}


template<class T>
gsXmlNode * putMatrixToXml ( gsMatrix<T> const & mat, gsXmlTree & data, std::string name) 
{
    std::ostringstream str;
    str << std::setprecision(FILE_PRECISION);
    // Write the matrix entries
    for (index_t i=0; i< mat.rows(); ++i)
    {
        for (index_t j=0; j<mat.cols(); ++j)
            str << mat(i,j)<< " ";
        str << "\n";
    }

{'fraction_non_alphanumeric': 0.16425520187405263, 'fraction_numerical': 0.0033071517155849523, 'mean_word_length': 2.2401785714285714, 'pattern_counts': {'":': 0, '<': 187, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695582', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2425, 'n_words': 617}