# -*- coding: utf-8 -*- # This file is part of Eigen, a lightweight C++ template library # for linear algebra. # # Copyright (C) 2009 Benjamin Schindler <bschindler@inf.ethz.ch> # # 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/.
# Pretty printers for Eigen::Matrix # This is still pretty basic as the python extension to gdb is still pretty basic. # It cannot handle complex eigen types and it doesn't support many of the other eigen types # This code supports fixed size as well as dynamic size matrices
# To use it: # # * Create a directory and put the file as well as an empty __init__.py in # that directory. # * Create a ~/.gdbinit file, that contains the following: # python # import sys # sys.path.insert(0, '/path/to/eigen/printer/directory') # from printers import register_eigen_printers # register_eigen_printers (None) # end
import gdb import re import itertools from bisect import bisect_left
# Basic row/column iteration code for use with Sparse and Dense matrices class _MatrixEntryIterator(object):
class EigenMatrixPrinter: "Print Eigen Matrix or Array of some kind"
def __init__(self, variety, val): "Extract all the necessary information"
# Save the variety (presumably "Matrix" or "Array") for later usage
self.variety = variety
# The gdb extension does not support value template arguments - need to extract them by hand
type = val.type if type.code == gdb.TYPE_CODE_REF:
type = type.target()
self.type = type.unqualified().strip_typedefs()
tag = self.type.tag
regex = re.compile('\<.*\>')
m = regex.findall(tag)[0][1:-1]
template_params = m.split(',')
template_params = [x.replace(" ", "") for x in template_params]
self.options = 0 # default value if len(template_params) > 3:
self.options = template_params[3];
self.rowMajor = (int(self.options) & 0x1)
self.innerType = self.type.template_argument(0)
self.val = val
# Fixed size matrices have a struct as their storage, so we need to walk through this
self.data = self.val['m_storage']['m_data'] if self.data.type.code == gdb.TYPE_CODE_STRUCT:
self.data = self.data['array']
self.data = self.data.cast(self.innerType.pointer())
class EigenSparseMatrixPrinter: "Print an Eigen SparseMatrix"
def __init__(self, val): "Extract all the necessary information"
type = val.type if type.code == gdb.TYPE_CODE_REF:
type = type.target()
self.type = type.unqualified().strip_typedefs()
tag = self.type.tag
regex = re.compile('\<.*\>')
m = regex.findall(tag)[0][1:-1]
template_params = m.split(',')
template_params = [x.replace(" ", "") for x in template_params]
self.options = 0 if len(template_params) > 1:
self.options = template_params[1];
row, col = super(EigenSparseMatrixPrinter._iterator, self).__next__()
# repeat calculations from SparseMatrix.h:
outer = row if self.rowMajor else col
inner = col if self.rowMajor else row
start = self.val['m_outerIndex'][outer]
end = ((start + self.val['m_innerNonZeros'][outer]) if self.val['m_innerNonZeros'] else
self.val['m_outerIndex'][outer+1])
# and from CompressedStorage.h:
data = self.val['m_data'] if start >= end:
item = 0 elif (end > start) and (inner == data['m_indices'][end-1]):
item = data['m_values'][end-1] else: # create Python index list from the target range within m_indices
indices = [data['m_indices'][x] for x in range(int(start), int(end)-1)] # find the index with binary search
idx = int(start) + bisect_left(indices, inner) if ((idx < end) and (data['m_indices'][idx] == inner)):
item = data['m_values'][idx] else:
item = 0
return ('[%d,%d]' % (row, col), item)
def children(self): if self.data: return self._iterator(self.rows(), self.cols(), self.val, self.rowMajor)
return iter([]) # empty matrix, for now
def rows(self): return self.val['m_outerSize'] if self.rowMajor else self.val['m_innerSize']
def cols(self): return self.val['m_innerSize'] if self.rowMajor else self.val['m_outerSize']
def to_string(self):
if self.data:
status = ("not compressed"if self.val['m_innerNonZeros'] else"compressed") else:
status = "empty"
dimensions = "%d x %d" % (self.rows(), self.cols())
layout = "row"if self.rowMajor else"column"
class EigenQuaternionPrinter: "Print an Eigen Quaternion"
def __init__(self, val): "Extract all the necessary information" # The gdb extension does not support value template arguments - need to extract them by hand
type = val.type if type.code == gdb.TYPE_CODE_REF:
type = type.target()
self.type = type.unqualified().strip_typedefs()
self.innerType = self.type.template_argument(0)
self.val = val
# Quaternions have a struct as their storage, so we need to walk through this
self.data = self.val['m_coeffs']['m_storage']['m_data']['array']
self.data = self.data.cast(self.innerType.pointer())
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.