# How will I get the number of terms in an object in Cadabra 2?

0 votes

e.g ,

Ex:={1}^R_{\mu \nu};


Here {1}^R_{\mu \nu} is perturbed Ricci Tensor

I have tried things like :

number_of_terms(Ex);


Cadabra 2 is showing syntax error.

asked

## 1 Answer

+1 vote

First of all, you cannot use Ex (with a capital E) as a name for an expression, because Ex is the name of the Cadabra expression object. Secondly, you cannot add pre-fix superscripts like this; the best you can do there is to use the LaTeXForm property. So you can do

R1{#}::LaTeXForm("{}^{1}R").
ex:= R1_{\mu\nu};


Now what do you wan to do next? This expression clearly only contains one term. Did you want to compute it for a particular metric and then determine the number of non-zero components?

answered by (48.6k points)

I am not sure if there is any algorithm called "numberofterms" in Cadabra2. Is it? It was in CADABRA1 where it was applied using @numberofterms. Perhaps the question was about how to get the number of terms in an expression that is sum of many terms.

Counting number of child elements is done with Python's standard len, so

ex:=A+B+C+D+E;
len(ex);


gives 5.

But note that this may not do what you want when you have single terms with indices, as it always counts the number of children, and when there is only a single term there is no \sum node at the top. So you get

ex:= A_{m n p q};
len(ex);


giving 4. I should probably re-instate number_of_terms to make this easier to catch...
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.678227424621582, "perplexity": 1035.5271509509384}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-39/segments/1568514575076.30/warc/CC-MAIN-20190922032904-20190922054904-00410.warc.gz"}