# Implement generalized local densities / current densities

## The Problem

Presently, Kwant has very nice support for calculating transport properties and manipulating the scattering matrix of a system. There is, however, less support for dealing with locally-defined quantities such as charge and current densities. For systems with a single degree of freedom (DOF) per site there is only one sensible density, but for systems with >1 DOF per site one could imagine more exotic densities (e.g. spin density for spin DOF, charge/quasiparticle density for electron/hole DOF in superconductors...).

In general the density on a site `i`

can be written as:

` ψ⁺_i ρ_i ψ_i`

where `ψ_i`

is a vector containing the wavefunction components on site `i`

, and `ρ_i`

is a Hermitian matrix that defines the density operator for site `i`

(it may be useful to have a density operator that varies from site to site, e.g. for defining spin density with a quantization axis perpendicular to some applied B field, for example).

Using the continuity equation one can define a general current operator from the `ρ`

for the current flowing between two sites `i`

and `j`

.

It would be nice to have support in Kwant for creating such density and current operators. The idea would be that such operators could then be applied to the scattering states calculated by `kwant.wave_function`

and the output could be plotted with `kwant.plotter.map`

(for densities) or `kwant.plotter.vector_map`

(for current densities)(see #5 (closed)).

## Proposed Solution

```
class Density:
"""Generalized density operator that can act on wavefunctions."""
def __init__(self, density):
"""
parameters
----------
density: ndarray or function
A Hermitian matrix that defines the density operator for a
single site. If the density operator is different for different
sites, a function can be provided. It should take a single Site
object and return a Hermitian matrix for the given site.
"""
# TODO: implement this
pass
def __call__(self, wavefunction):
"""Calculate the expectation value of this operator with a wavefunction."""
# TODO: implement this
pass
class CurrentDensity:
"""Generalized current operator that can act on wavefunctions"""
def __init__(self, density):
"""
parameters
----------
density: Density or ndarray or functions
see the __init__ docstring for Density
"""
def __call__(self, wavefunction):
"""Calculate the expectation value of this operator with a wavefunction."""
# TODO: implement this
pass
```